CAD定义图块(com接口c#语言)
定义图块
c#将选中的实体做成块实现代码:
MxDrawSelectionSet ss = new MxDrawSelectionSet(); ss.Select(MCAD_McSelect.mcSelectionSetUserSelect, null , null , null ); List<Int64> aryId = new List<Int64>(); for (Int32 i = 0; i < ss.Count; i++) { MxDrawEntity ent = ss.Item(i); if (ent != null ) { aryId.Add(ent.ObjectID); ent.Close(); } } MxDrawDatabase curDatabase = (MxDrawDatabase)(mxdraw.GetDatabase()); double dMinX = 0, dMinY = 0, dMaxX = 0, dMaxY = 0; bool isFirstEnt = true ; for (int l = 0; l < aryId.Count; l++) { MxDrawMcDbObject pObj = curDatabase.ObjectIdToObject(aryId[l]); if (pObj == null ) continue ; MxDrawPoint pt1Ob, pt2Ob; MxDrawEntity pEnt = (MxDrawEntity)pObj; if (pEnt == null ) continue ; pEnt.GetBoundingBox(out pt1Ob, out pt2Ob); MxDrawPoint minPt = (MxDrawPoint)pt1Ob; MxDrawPoint maxPt = (MxDrawPoint)pt2Ob; if (minPt != null && maxPt != null ) { if (isFirstEnt) { dMinX = minPt.x; dMinY = minPt.y; dMaxX = maxPt.x; dMaxY = maxPt.y; isFirstEnt = false ; } else { if (dMinX > minPt.x) dMinX = minPt.x; if (dMinY > minPt.y) dMinY = minPt.y; if (dMaxX < maxPt.x) dMaxX = maxPt.x; if (dMaxY < maxPt.y) dMaxY = maxPt.y; } } } if (isFirstEnt) { // 没有实体 return 0; } MxDrawPoint pos = new MxDrawPoint(); pos.x = dMinX + (dMaxX - dMinX) / 2.0; pos.y = dMinY + (dMaxY - dMinY) / 2.0; // 检查新创建的块名,在当前数据库是否已经存在. MxDrawBlockTable blkTable = curDatabase.GetBlockTable(); MxDrawBlockTableRecord curSpace = curDatabase.CurrentSpace(); String sNewBlakName; MxDrawBlockTableRecord blkNewRec = blkTable.Add( "" ); sNewBlakName = blkNewRec.Name; blkNewRec.Origin = pos; for (int l = 0; l < aryId.Count; l++) { blkNewRec.AddCloneEntity(aryId[l]); // 把以前实体删除 。 MxDrawMcDbObject pObj = curDatabase.ObjectIdToObject(aryId[l]); if (pObj == null ) continue ; pObj.Erase(); } if (blkNewRec != null ) { MxDrawBlockReference blkRef = curSpace.InsertBlock(pos.x,pos.y, sNewBlakName, 1.0, 0.0); MxDrawAttribute attrib = blkRef.AppendAttribute(); attrib.Position = pos; attrib.AlignmentPoint = pos; attrib.Oblique = 0.0; attrib.Rotation = 0.0; attrib.Height = 2.0; attrib.TrueColor.SetRGB(255,0,0); // attrib.TextString = "这是一个属性文字的测试"; attrib.Tag = "TestTag" ; attrib.IsInvisible = false ; blkRef.Position = blkRef.Position; return blkRef.ObjectID; } return 0;
|