- 积分
- 1839
- 明经币
- 个
- 注册时间
- 2012-3-11
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
楼主 |
发表于 2024-5-31 12:42:51
|
显示全部楼层
- void
- ArxDbgCmdTests::registerCommandLineFunctions(AcEdCommandStack* cmdStack, LPCTSTR appname)
- {
- cmdStack->addCommand(appname, _T("LayoutEntByLayer"), _T("LayoutEntByLayer"), (ACRX_CMD_MODAL | ACRX_CMD_USEPICKSET), &LayoutEntitiesByLayer);
- }
- void ArxDbgCmdTests::LayoutEntitiesByLayer()
- {
- acutPrintf(_T("\n\n--------------------QQ:1005144760-------------------------------------"));
- acutPrintf(_T("\n定制CAD功能开发,支持Autocad、Revit、 NX、 Solidwork、CATIA等平台. QQ:1005144760"));
- acutPrintf(_T("\n--------------------QQ:1005144760-------------------------------------\n\n"));
- acutPrintf(_T("\n根据图层排序,排序规则: 实体包围盒左下点坐标和排序方向确定(Y轴方向排序:Y坐标从小到大, X轴方向排序:X坐标从由小到大)"));
- CString strPrompt;
- strPrompt.Format(_T("\n选择块实体或 <全部选择>"));
- ArxDbgRbList filter;
- ArxDbgSelSet ss;
- AcDbObjectIdArray objIds;
- ArxDbgSelSet::SelSetStatus selStatus = ss.userSelect(strPrompt, NULL, filter.data());
- if (selStatus == ArxDbgSelSet::kNone)
- {
- // 选择图中所有的块实体
- ss.allSelect(filter.data());
- ss.asArray(objIds);
- }
- else if (selStatus == ArxDbgSelSet::kSelected)
- {
- // 用户选择了文本转换选择集
- ss.asArray(objIds);
- }
- if (objIds.length() == 0)
- {
- acutPrintf(_T("\n没有实体,命令退出!"));
- return;
- }
- std::map<AcDbObjectId, AcDbObjectIdArray> layerToObjIds;
- std::map<AcDbObjectId, AcDbExtents> layerToExtent;
- for (int i = 0; i < objIds.logicalLength(); i++)
- {
- AcDbEntityPointer pEnt(objIds[i], AcDb::kForRead);
- if (pEnt.openStatus() != Acad::eOk)
- {
- acutPrintf(_T("\nWarning: open error. ignore one entity."));
- continue;
- }
- AcDbExtents ex;
- if (Acad::eOk != pEnt->getGeomExtents(ex))
- {
- acutPrintf(_T("\nWarning: geom extent error. ignore this entity."));
- continue;
- }
- layerToObjIds[pEnt->layerId()].append(objIds[i]);
- layerToExtent[pEnt->layerId()].addExt(ex);
- }
- size_t nLayerCount = layerToObjIds.size();
- acutPrintf(_T("\n共选择%d实体, 分布在%d图层"), objIds.logicalLength(), nLayerCount);
- if (nLayerCount <= 1)
- {
- acutPrintf(_T("\n 图层数量过少,不需要排序!"));
- return;
- }
- bool bIsYAxis = true;
- ArxDbgUiPrKeyWordDef prKeyDirection(_T("选择排序方向[X轴(X)/Y轴(Y)]"), _T("X Y"), _T("Y"));
- if (prKeyDirection.go() != ArxDbgUiPrBase::kOk)
- return;
- if (prKeyDirection.isKeyWordPicked(_T("X")))
- bIsYAxis = Adesk::kFalse;
- AcDbExtents exTotal;
- std::map<AcDbObjectId, AcDbExtents> layerToExtentToSort;
- layerToExtentToSort = layerToExtent;
- AcDbObjectIdArray sortedLayers;
- while (layerToExtentToSort.size() > 0)
- {
- if (layerToExtentToSort.size() == 1)
- {
- sortedLayers.append(layerToExtentToSort.begin()->first);
- break;
- }
- std::map<AcDbObjectId, AcDbExtents>::iterator it = layerToExtentToSort.begin();
- AcDbExtents exMin = it->second;
- AcDbObjectId idMin = it->first;
- it++;
- for (; it != layerToExtentToSort.end(); it++)
- {
- AcDbExtents exCur = it->second;
- if (bIsYAxis)
- {
- if (exMin.minPoint().y - exCur.minPoint().y > -1e-6)
- {
- exMin = exCur;
- idMin = it->first;
- }
- }
- else
- {
- if (exMin.minPoint().x - exCur.minPoint().x > -1e-6)
- {
- exMin = exCur;
- idMin = it->first;
- }
- }
- }
- sortedLayers.append(idMin);
- layerToExtentToSort.erase(idMin);
- exTotal.addExt(exMin);
- }
- ArxDbgUiPrPoint prInsert(_T("\n指定布局插入点"), NULL);
- if (prInsert.go() != ArxDbgUiPrBase::kOk)
- {
- return;
- }
- CString str;
- if (bIsYAxis)
- {
- str.Format(_T("\n指定Y轴间距"));
- }
- else
- {
- str.Format(_T("\n指定X轴间距"));
- }
- double dOffset = 0.0;
- ArxDbgUiPrDistDef prDist(_T("\n指定偏移距离"), NULL, ArxDbgUiPrDist::kNoNeg, prInsert.value(), 0.0);
- prDist.setAllowNone(true);
- ArxDbgUiPrBase::Status st = prDist.go();
- if (st == ArxDbgUiPrBase::kNone)
- {
- dOffset = 0.0;
- }
- else if (st == ArxDbgUiPrBase::kOk)
- {
- dOffset = prDist.value();
- }
- else
- {
- return;
- }
- AcGePoint3d ptInsert = prInsert.value();
- AcGeVector3d offVec = AcGeVector3d::kYAxis * dOffset;
- if (!bIsYAxis)
- {
- offVec = AcGeVector3d::kXAxis * dOffset;
- }
- bool bCreateLayerName = false;
- ArxDbgUtils::yesNoPromptDef(_T("创建图层名字?"), bCreateLayerName, false);
- AcGeVector3d lastOffVec;
- for (int i = 0; i < sortedLayers.logicalLength(); i++)
- {
- AcDbExtents ex = exTotal; //layerToExtent.at(sortedLayers[i]); 按每个图形的Extend计算.
- AcGeMatrix3d matrix = AcGeMatrix3d::translation(ptInsert - ex.minPoint());
- //For Test
- // AcDbExtents extmp = ex;
- // extmp.transformBy(matrix);
- // drawExtent(extmp);
- if (bCreateLayerName)
- {
- AcGePoint3d ptLeftBottom = ex.minPoint();
- ptLeftBottom.transformBy(matrix);
- AcDbLayerTableRecordPointer pLayerTblRec(sortedLayers[i], AcDb::kForRead);
- if (pLayerTblRec.openStatus() == Acad::eOk)
- {
- AcString strName;
- pLayerTblRec->getName(strName);
- if (!strName.isEmpty())
- {
- AcDbMText* pNewText = new AcDbMText;
- pNewText->setLocation(ptLeftBottom); // set to be just to the right of the logo
- double dHeight = (ex.maxPoint() - ex.minPoint()).dotProduct(AcGeVector3d::kYAxis);
- pNewText->setTextHeight(dHeight * 0.05);
- //pNewText->setWidth(0.0);
- pNewText->setContents(strName);
- pNewText->setAttachment(AcDbMText::kBottomLeft);
- ArxDbgUtils::addToCurrentSpaceAndClose(pNewText);
- }
- }
- }
- const AcDbObjectIdArray& entIds = layerToObjIds[sortedLayers[i]];
- for (int j = 0; j < entIds.logicalLength(); j++)
- {
- AcDbEntityPointer pEnt(entIds[j], AcDb::kForWrite);
- if (pEnt.openStatus() == Acad::eOk)
- {
- pEnt->transformBy(matrix);
- }
- }
- if (bIsYAxis)
- {
- ptInsert += AcGeVector3d(0, ex.maxPoint().y - ex.minPoint().y, 0);
- }
- else
- {
- ptInsert += AcGeVector3d(ex.maxPoint().x - ex.minPoint().x, 0, 0);
- }
- ptInsert += offVec;
- }
- }
|
|