- 积分
- 2765
- 明经币
- 个
- 注册时间
- 2015-12-1
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
折腾了很久最后发现是建块的时候没有对齐坐标原点。多谢 AutoCAD.NetApi群(2474812)的[C#]非狐、[c#lisp]惊惊、[lisp C#]若海、小贱贱等前辈或同学的帮助,现把实现代码贴出来,希望能帮到大家。
- /// <summary>
- /// 创建图块图块,有重名图块的时候会被覆盖
- /// </summary>
- public static ObjectId CreatBlockBySelection(string blockName)
- {
- Document acDoc = App.DocumentManager.MdiActiveDocument;
- acDoc.LockDocument();
- Database acCurDb = acDoc.Database;
- ObjectId rtn= ObjectId.Null;
- PromptSelectionOptions acSelOpt = new PromptSelectionOptions();
- acSelOpt.MessageForAdding = "\n-->>请选择要创建块的图形";
- PromptSelectionResult acSSPrompt = acDoc.Editor.SelectImplied();
- if (acSSPrompt.Status != PromptStatus.OK)
- acSSPrompt = acDoc.Editor.GetSelection(acSelOpt);
- if (acSSPrompt.Status == PromptStatus.OK)
- {
- PromptPointOptions ptOpt = new PromptPointOptions("\n-->>请选择块基点");
- PromptPointResult ptRt = acDoc.Editor.GetPoint(ptOpt);
- Point3d insertPoint = ptRt.Value;
- if (ptRt.Status == PromptStatus.Cancel) rtn=ObjectId.Null;
- else
- {
- SelectionSet acSSet = acSSPrompt.Value;
- using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
- {
- BlockTable blkTable = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite) as BlockTable;
- BlockTableRecord tableRecord = acTrans.GetObject(blkTable[GetSpaceType()], OpenMode.ForWrite) as BlockTableRecord;
- LayerStateManager lsm = acDoc.Database.LayerStateManager;
- string lsmName = DateTime.Now.Millisecond.ToString();
- if (lsm.HasLayerState(lsmName)) lsm.DeleteLayerState(lsmName);
- lsm.SaveLayerState(lsmName, LayerStateMasks.Frozen | LayerStateMasks.Locked, ObjectId.Null);
- LayerTable lyTable = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForWrite) as LayerTable;
- foreach (ObjectId lyrcd in lyTable)
- {
- LayerTableRecord layer = acTrans.GetObject(lyrcd, OpenMode.ForWrite) as LayerTableRecord;
- if (layer.Name != "0") layer.IsFrozen = false;
- layer.IsLocked = false;
- }
- BlockTableRecord newBlockTableRecord;
- if (blkTable.Has(blockName))
- {
- newBlockTableRecord = acTrans.GetObject(blkTable[blockName], OpenMode.ForWrite) as BlockTableRecord;
- foreach (ObjectId id in newBlockTableRecord)
- {
- Entity ent = acTrans.GetObject(id, OpenMode.ForWrite) as Entity;
- ent.Erase();
- }
- newBlockTableRecord.AssumeOwnershipOf(new ObjectIdCollection(acSSet.GetObjectIds()));
- Matrix3d mt = Matrix3d.Displacement(Point3d.Origin - insertPoint);
- foreach (ObjectId id in newBlockTableRecord)
- {
- Entity ent = acTrans.GetObject(id, OpenMode.ForWrite) as Entity;
- ent.TransformBy(mt);
- }
- }
- else
- {
- newBlockTableRecord = new BlockTableRecord();
- newBlockTableRecord.Name = blockName;
- newBlockTableRecord.Origin = Point3d.Origin;
- blkTable.Add(newBlockTableRecord);
- acTrans.AddNewlyCreatedDBObject(newBlockTableRecord, true);
- Matrix3d mt = Matrix3d.Displacement(Point3d.Origin - insertPoint);
- ObjectIdCollection objCol = new ObjectIdCollection();
- foreach (ObjectId id in acSSet.GetObjectIds())
- {
- Entity ent = acTrans.GetObject(id, OpenMode.ForWrite) as Entity;
- ent.TransformBy(mt);
- objCol.Add(id);
- }
- newBlockTableRecord.AssumeOwnershipOf(objCol);
- }
- rtn = blkTable[blockName];
- BlockReference blockReference = new BlockReference(insertPoint, rtn);
- tableRecord.AppendEntity(blockReference);
- acTrans.AddNewlyCreatedDBObject(blockReference, true);
- lsm.RestoreLayerState(lsmName, ObjectId.Null, 0, LayerStateMasks.Frozen | LayerStateMasks.Locked);
- lsm.DeleteLayerState(lsmName);
- acTrans.Commit();
- }
- }
- acDoc.Editor.Regen();
- }
- return rtn;
- }
- [CommandMethod("tt1", CommandFlags.UsePickSet | CommandFlags.Redraw | CommandFlags.Modal | CommandFlags.NoBlockEditor)]
- public static void tt1()
- {
- CreatBlockBySelection("newBlock");
- }
|
|