objectARX 布局裁图


 1 #pragma once
2
3 // ----------------------------------------------------------------------------------------------------------------
4 // Summary: 布局裁图
5 // ----------------------------------------------------------------------------------------------------------------
6 class CLayoutCutDrawing
7 {
8 public:
9 CLayoutCutDrawing(void);
10 ~CLayoutCutDrawing(void);
11
12 // --------------------------------------------------------------------------------------------
13 // Summary: 绘制布局裁图
14 // Parameters:
15 // pt1 - 裁图区域所在的外包最小点
16 // pt2 - 裁图区域所在的外包最大点
17 // ptInsertTopLeft - 视口的左下脚点
18 // dScaleFactor - 缩放比例
19 // dAngle - 裁图角度
20 // pBoundary - 裁图边界
21 // Returns:
22 // 成功返回 Acad::eOk, 否则返回 Acad::ErrorStatus 错误状态
23 // --------------------------------------------------------------------------------------------
24 static Acad::ErrorStatus DrawLayout(const AcGePoint3d& pt1, const AcGePoint3d& pt2, const AcGePoint3d& ptInsertBottomLeft, LPCTSTR lpszLayoutName, double dScaleFactor, double dAngle, AcDbPolyline* pBoundary);
25
26 // --------------------------------------------------------------------------------------------
27 // Summary: 创建新布局
28 // Parameters:
29 // lpszName - 布局的名称
30 // layoutId - 布局id
31 // blkTabRecId - 布局块表记录
32 // Returns:
33 // 成功返回 Acad::eOk, 否则返回 Acad::ErrorStatus 错误状态
34 // --------------------------------------------------------------------------------------------
35 static Acad::ErrorStatus CreateNewLayout(LPCTSTR lpszName, AcDbObjectId& layoutId, AcDbObjectId& blkTabRecId);
36
37 protected:
38
39 // --------------------------------------------------------------------------------------------
40 // Summary: 遍历所有布局
41 // --------------------------------------------------------------------------------------------
42 Acad::ErrorStatus AllLayout();
43
44 // --------------------------------------------------------------------------------------------
45 // Summary: 查找指定名称的布局
46 // Parameters:
47 // lpszName - 布局的名称
48 // layoutId - 布局id
49 // blkTabRecId - 布局块表记录
50 // Returns:
51 // 成功返回 Acad::eOk, 否则返回 Acad::ErrorStatus 错误状态
52 // --------------------------------------------------------------------------------------------
53 static Acad::ErrorStatus FindLayout(LPCTSTR lpszName, AcDbObjectId& layoutId, AcDbObjectId& blkTabRecId);
54 };

 

 

 

 

 

实现文件:

  1 CLayoutCutDrawing::CLayoutCutDrawing(void)
2 {
3 }
4
5 CLayoutCutDrawing::~CLayoutCutDrawing(void)
6 {
7 }
8
9 Acad::ErrorStatus CLayoutCutDrawing::DrawLayout(const AcGePoint3d& pt1, const AcGePoint3d& pt2, const AcGePoint3d& ptInsertBottomLeft, LPCTSTR lpszLayoutName, double dScaleFactor/* = 1.0*/, double dAngle /*= 0.0*/, AcDbPolyline* pBoundary /*= NULL*/)
10 {
11 AcGeMatrix3d mat;
12 // mat.setToScaling(1 / dScaleFactor, pt1);
13 mat *= AcGeMatrix3d::translation(ptInsertBottomLeft - pt1);
14
15 AcGeVector3d dirRotate = AcGeVector3d::kXAxis;
16 dirRotate.rotateBy(dAngle, AcGeVector3d::kZAxis);
17
18 AcDbExtents ext(pt1, pt2);
19 AcGePoint3d ptGeoMid = AcGePoint3d((pt2.x + pt1.x) / 2.0, (pt2.y + pt1.y) / 2.0, 0.0);
20 AcGePoint3d ptModelCenter = ptGeoMid;
21
22 mat *= AcGeMatrix3d::rotation(-dAngle, AcGeVector3d::kZAxis, ptModelCenter);
23
24 if (NULL == pBoundary)
25 {
26 return Acad::eNotImplementedYet;
27 }
28
29 AcDbObjectId layerId = pBoundary->layerId();
30 AcDbObjectIdArray layerIdArr;
31 layerIdArr.append(layerId);
32
33 actrTransactionManager->startTransaction();
34 try
35 {
36 // 新建布局
37 AcDbObjectId viewportId;
38 AcDbObjectId layoutId;
39 AcDbObjectId blkTabRecId;
40 AcDbObjectId plineId;
41
42 FindLayout(lpszLayoutName, layoutId, blkTabRecId);
43
44 // 新建视口
45 AcDbViewport* pViewPort = new AcDbViewport;
46 pViewPort->setCustomScale(dScaleFactor);
47 pViewPort->setCenterPoint(/*ptTKCen*/ptModelCenter);
48
49 // 设置视口属性
50 double dHeight = fabs(ext.maxPoint().y - ext.minPoint().y);
51 pViewPort->setWidth(fabs(ext.maxPoint().x - ext.minPoint().x));
52 pViewPort->setHeight(dHeight);
53 // pViewPort->setLayer(m_ComLayer_BuJuShiKou);
54 pViewPort->setTwistAngle(-dAngle);
55 pViewPort->setViewTarget(ptModelCenter);// 设置视口目标
56 pViewPort->setViewHeight(dHeight);
57 pViewPort->setCustomScale(dScaleFactor);
58 pViewPort->freezeLayersInViewport(layerIdArr); // 在当前视口中冻结裁图线所在的图层
59
60 {
61 // 在图纸空间中加入视口
62 AcDbObjectPointer<AcDbBlockTableRecord> spBlk(blkTabRecId, AcDb::kForWrite);
63 EOK_RETURN_ERROR(spBlk.openStatus());
64 spBlk->appendAcDbEntity(viewportId, pViewPort);
65
66 Acad::ErrorStatus es = pViewPort->setOn();
67 }
68
69 {
70 // 在图纸空间中加入闭合多边形pline线
71 AcDbPolyline* pTempLine = AcDbPolyline::cast(pBoundary->clone()) ;
72 pTempLine->setClosed(Adesk::kTrue);
73
74 AcDbObjectPointer<AcDbBlockTableRecord> spBlk(blkTabRecId, AcDb::kForWrite);
75 EOK_RETURN_ERROR(spBlk.openStatus());
76 spBlk->appendAcDbEntity(plineId, pTempLine);
77
78 pTempLine->transformBy(mat);
79
80 pTempLine->close();
81 }
82
83 pViewPort->transformBy(mat);
84 EOK_RETURN_ERROR(pViewPort->setNonRectClipEntityId(plineId));
85 EOK_RETURN_ERROR(pViewPort->setNonRectClipOn());
86 EOK_RETURN_ERROR(pViewPort->setLocked());
87 EOK_RETURN_ERROR(pViewPort->close());
88 }
89 catch (const Acad::ErrorStatus err)
90 {
91 actrTransactionManager->endTransaction();
92 }
93 catch (...)
94 {
95 acutPrintf(_T("未知错误!"));
96 actrTransactionManager->endTransaction();
97 }
98
99 actrTransactionManager->endTransaction();
100
101 // 有时生成的裁图在视口以外的地方,需要缩放才能看到
102   // 但是每生成一副裁图,就要闪烁,先屏蔽了, 放在命令结束时做一次缩放
103   // acedCommand(RTSTR, _T("ZOOM"), RTSTR, _T("E"), 0);
104 ads_regen();
105
106 return Acad::eOk;
107 }
108
109 Acad::ErrorStatus CLayoutCutDrawing::CreateNewLayout(LPCTSTR lpszName, AcDbObjectId& layoutId, AcDbObjectId& blkTabRecId)
110 {
114 AcApLayoutManager* pLayMan = (AcApLayoutManager *) acdbHostApplicationServices()->layoutManager();
115 if (!pLayMan)
116 {
117 return Acad::eNotImplementedYet;
118 }
119
120 AcDbLayout* pLayout = NULL;
121 if (pLayout = pLayMan->findLayoutNamed(lpszName))
122 {
123 layoutId = pLayout->objectId();
124 blkTabRecId = pLayout->getBlockTableRecordId();
125
126 pLayMan->setCurrentLayoutId(layoutId);
127
128 return Acad::eNotImplementedYet;
129 }
130 else
131 {
132 // pLayMan->createLayout(lpszName, layoutId, blkTabRecId);
133 // pLayMan->updateLayoutTabs();
134 // pLayMan->setCurrentLayoutId(layoutId);
135
136 Acad::ErrorStatus es = Acad::eOk;
137 AcDbBlockTable* pBlockTable;
138 es = acdbCurDwg()->getSymbolTable(pBlockTable,AcDb::kForWrite);
139 EOK_RETURN_ERROR(es);
140
141 AcDbBlockTableRecord* pBlockTableRecord = new AcDbBlockTableRecord;
142 pBlockTableRecord->setName(_T("*Paper_Space"));
143 pBlockTable->add(blkTabRecId, pBlockTableRecord);
144 pBlockTable->close();
145 pBlockTableRecord->close();
146
147 pLayout = new AcDbLayout;
148 if(!pLayout)
149 {
150 return es;
151 }
152 es = pLayout->setLayoutName(lpszName);
153 if (es != Acad::eOk)
154 {
155 delete pLayout;
156 return es;
157 }
158
159 es = pLayout->addToLayoutDict(acdbCurDwg(), blkTabRecId);
160 if (es != Acad::eOk)
161 {
162 delete pLayout;
163 return es;
164 }
165
166 pLayout->close();
167 pLayMan->updateLayoutTabs();
168 pLayMan->setCurrentLayout(lpszName);
169 }
170
171 return Acad::eOk;
172 }
173
174 Acad::ErrorStatus CLayoutCutDrawing::AllLayout()
175 {
176 //遍历所有布局
177 {
178 //获得布局字典,布局字典里包含了模型和布局
179 AcDbDictionary *pLayoutDic=NULL;
180 Acad::ErrorStatus es=acdbHostApplicationServices()
181 ->workingDatabase()->getLayoutDictionary(pLayoutDic,AcDb::kForRead);
182 if(es != Acad::eOk)
183 {
184 pLayoutDic->close();
185 acutPrintf(_T("\n获取图纸空间字典错误"));
186 return Acad::eOk;
187 }
188 if(pLayoutDic->numEntries()==0)
189 {
190 pLayoutDic->close();
191 acutPrintf(_T("\n当前图形不存在图纸空间"));
192 return Acad::eOk;
193 }
194
195 //创建布局字典遍历器
196 AcDbDictionaryIterator *pLayoutDicItr=pLayoutDic->newIterator();
197
198 //创建循环,遍历布局
199 for(int i = 1; !pLayoutDicItr->done(); pLayoutDicItr->next())
200 {
201 acutPrintf(_T("\n第[ %d ]个布局,布局名称为[ %s ]"),i,pLayoutDicItr->name());
202 i++;
203 }
204 delete pLayoutDicItr; //删除遍历器对象
205 pLayoutDic->close();
206 }
207
208 return Acad::eOk;
209 }
210
211 Acad::ErrorStatus CLayoutCutDrawing::FindLayout(LPCTSTR lpszName, AcDbObjectId& layoutId, AcDbObjectId& blkTabRecId)
212 {
213 AcApLayoutManager* pLayMan = (AcApLayoutManager *) acdbHostApplicationServices()->layoutManager();
214 if (!pLayMan)
215 {
216 return Acad::eNotImplementedYet;
217 }
218
219 AcDbLayout* pLayout = NULL;
220 if (pLayout = pLayMan->findLayoutNamed(lpszName))
221 {
222 layoutId = pLayout->objectId();
223 blkTabRecId = pLayout->getBlockTableRecordId();
224
225 pLayMan->setCurrentLayoutId(layoutId);
226
227 return Acad::eOk;
228 }
229
230 return Acad::eNotImplementedYet;
231 }




免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM