在创建对象,然后使用该方法将其追加到对象之后,可以插入当前图形的块表中定义的块。当您创建一个新的 时,可以向构造函数传递要插入的块引用的原点以及几何图形应从中继承的对象的原点。BlockReferenceBlockTableRecordAppendEntityBlockReferenceObjectIDBlockTableRecord 还可以使用在内存中打开图形文件的方法将整个图形文件插入到当前图形中,然后使用“插入”方法将内存中的图形插入到当前图形中。将整个图形插入到另一个图形中时,AutoCAD 会像处理任何其他块参照一样处理插入的图形。后续插入引用具有不同位置、比例和旋转设置的块定义(包含块的几何描述)。ReadDwgFile 如果在插入原始图形后对其进行更改,则更改对插入的块没有影响。如果希望插入的块反映对原始图形所做的更改,则可以通过重新插入原始图形来重新定义块。 默认情况下,AutoCAD 在插入图形文件时使用坐标 (0, 0, 0) 作为基点。可以通过打开原始图形并使用该方法为 INSBASE 系统变量指定不同的插入基点来更改图形的基点。AutoCAD 将在下次插入图形时使用新基点。SetSystemVariable 如果插入的图形包含对象,则这些对象不包含在当前图形的块定义中。要使用其他图形中的对象,请打开原始图形并将对象定义为块。可以将图形插入到图纸空间或模型空间中的另一图形中。PaperSpacePaperSpacePaperSpace 无法迭代块引用以查找组成它的原始对象。但是,您可以迭代原始块定义,也可以将块引用分解为其原始组件。 您还可以使用该对象插入块数组。此对象类型不会像使用 BlockReference 对象那样在图形中插入单个块,而是插入指定块的数组。MInsertBlock 有关插入模块的更多信息,请参阅产品帮助系统中的“关于插入模块”。 定义和插入块此示例定义一个块,并向块定义添加一个圆圈。然后,它将块作为块参照插入到图形中。 VB.NETImports Autodesk.AutoCAD.Runtime Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.Geometry <CommandMethod("InsertingABlock")> _ Public Sub InsertingABlock() ' Get the current database and start a transaction Dim acCurDb As Autodesk.AutoCAD.DatabaseServices.Database acCurDb = Application.DocumentManager.MdiActiveDocument.Database Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction() ' Open the Block table for read Dim acBlkTbl As BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) Dim blkRecId As ObjectId = ObjectId.Null If Not acBlkTbl.Has("CircleBlock") Then Using acBlkTblRec As New BlockTableRecord acBlkTblRec.Name = "CircleBlock" ' Set the insertion point for the block acBlkTblRec.Origin = New Point3d(0, 0, 0) ' Add a circle to the block Using acCirc As New Circle acCirc.Center = New Point3d(0, 0, 0) acCirc.Radius = 2 acBlkTblRec.AppendEntity(acCirc) acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite) acBlkTbl.Add(acBlkTblRec) acTrans.AddNewlyCreatedDBObject(acBlkTblRec, True) End Using blkRecId = acBlkTblRec.Id End Using Else blkRecId = acBlkTbl("CircleBlock") End If ' Insert the block into the current space If blkRecId <> ObjectId.Null Then Using acBlkRef As New BlockReference(New Point3d(0, 0, 0), blkRecId) Dim acCurSpaceBlkTblRec As BlockTableRecord acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) acCurSpaceBlkTblRec.AppendEntity(acBlkRef) acTrans.AddNewlyCreatedDBObject(acBlkRef, True) End Using End If ' Save the new object to the database acTrans.Commit() ' Dispose of the transaction End Using End Sub C#using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; [CommandMethod("InsertingABlock")] public void InsertingABlock() { // Get the current database and start a transaction Database acCurDb; acCurDb = Application.DocumentManager.MdiActiveDocument.Database; using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; ObjectId blkRecId = ObjectId.Null; if (!acBlkTbl.Has("CircleBlock")) { using (BlockTableRecord acBlkTblRec = new BlockTableRecord()) { acBlkTblRec.Name = "CircleBlock"; // Set the insertion point for the block acBlkTblRec.Origin = new Point3d(0, 0, 0); // Add a circle to the block using (Circle acCirc = new Circle()) { acCirc.Center = new Point3d(0, 0, 0); acCirc.Radius = 2; acBlkTblRec.AppendEntity(acCirc); acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite); acBlkTbl.Add(acBlkTblRec); acTrans.AddNewlyCreatedDBObject(acBlkTblRec, true); } blkRecId = acBlkTblRec.Id; } } else { blkRecId = acBlkTbl["CircleBlock"]; } // Insert the block into the current space if (blkRecId != ObjectId.Null) { using (BlockReference acBlkRef = new BlockReference(new Point3d(0, 0, 0), blkRecId)) { BlockTableRecord acCurSpaceBlkTblRec; acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord; acCurSpaceBlkTblRec.AppendEntity(acBlkRef); acTrans.AddNewlyCreatedDBObject(acBlkRef, true); } } // Save the new object to the database acTrans.Commit(); // Dispose of the transaction } } VBA/ActiveX 代码参考Sub InsertingABlock() ' Define the block Dim blockObj As AcadBlock Dim insertionPnt(0 To 2) As Double insertionPnt(0) = 0 insertionPnt(1) = 0 insertionPnt(2) = 0 Set blockObj = ThisDrawing.Blocks.Add(insertionPnt, "CircleBlock") ' Add a circle to the block Dim circleObj As AcadCircle Dim center(0 To 2) As Double Dim radius As Double center(0) = 0 center(1) = 0 center(2) = 0 rad = 2 Set circleObj = blockObj.AddCircle(center, rad) ' Insert the block Dim blockRefObj As AcadBlockReference insertionPnt(0) = 0 insertionPnt(1) = 0 insertionPnt(2) = 0 Set blockRefObj = ThisDrawing.ModelSpace.InsertBlock(insertionPnt, "CircleBlock", 1, 1, 1, 0) End Sub 父主题: |
|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2025-1-8 19:09
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.