使用任何对象方法和属性来重新定义块。重新定义块时,图形中对该块的所有参照都会立即更新以反映新定义。Block 重新定义会影响块的先前和未来插入。常量属性将丢失,并被任何新的常量属性替换。即使新块没有属性,变量属性也保持不变。 重新定义块定义中的对象此示例创建一个块,并在该块的定义中添加一个圆圈。然后,将块作为块参照插入到图形中。模块定义中的圆圈将更新,块参照将自动更新。 VB.NETImports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
<CommandMethod("RedefiningABlock")> _
Public Sub RedefiningABlock()
' 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)
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)
' Insert the block into the current space
Using acBlkRef As New BlockReference(New Point3d(0, 0, 0), acBlkTblRec.Id)
Dim acModelSpace As BlockTableRecord
acModelSpace = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite)
acModelSpace.AppendEntity(acBlkRef)
acTrans.AddNewlyCreatedDBObject(acBlkRef, True)
MsgBox("CircleBlock has been created.")
End Using
End Using
End Using
Else
' Redefine the block if it exists
Dim acBlkTblRec As BlockTableRecord = _
acTrans.GetObject(acBlkTbl.Item("CircleBlock"), OpenMode.ForWrite)
' Step through each object in the block table record
For Each objID As ObjectId In acBlkTblRec
Dim dbObj As DBObject = acTrans.GetObject(objID, OpenMode.ForRead)
' Revise the circle in the block
If TypeOf dbObj Is Circle Then
Dim acCirc As Circle = dbObj
acTrans.GetObject(objID, OpenMode.ForWrite)
acCirc.Radius = acCirc.Radius * 2
End If
Next
' Update existing block references
For Each objID As ObjectId In acBlkTblRec.GetBlockReferenceIds(False, True)
Dim acBlkRef As BlockReference = acTrans.GetObject(objID, OpenMode.ForWrite)
acBlkRef.RecordGraphicsModified(True)
Next
MsgBox("CircleBlock has been revised.")
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("RedefiningABlock")]
public void RedefiningABlock()
{
// 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;
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);
// Insert the block into the current space
using (BlockReference acBlkRef = new BlockReference(new Point3d(0, 0, 0), acBlkTblRec.Id))
{
BlockTableRecord acModelSpace;
acModelSpace = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
acModelSpace.AppendEntity(acBlkRef);
acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
Application.ShowAlertDialog("CircleBlock has been created.");
}
}
}
}
else
{
// Redefine the block if it exists
BlockTableRecord acBlkTblRec =
acTrans.GetObject(acBlkTbl["CircleBlock"], OpenMode.ForWrite) as BlockTableRecord;
// Step through each object in the block table record
foreach (ObjectId objID in acBlkTblRec)
{
DBObject dbObj = acTrans.GetObject(objID, OpenMode.ForRead) as DBObject;
// Revise the circle in the block
if (dbObj is Circle)
{
Circle acCirc = dbObj as Circle;
acTrans.GetObject(objID, OpenMode.ForWrite);
acCirc.Radius = acCirc.Radius * 2;
}
}
// Update existing block references
foreach (ObjectId objID in acBlkTblRec.GetBlockReferenceIds(false, true))
{
BlockReference acBlkRef = acTrans.GetObject(objID, OpenMode.ForWrite) as BlockReference;
acBlkRef.RecordGraphicsModified(true);
}
Application.ShowAlertDialog("CircleBlock has been revised.");
}
// Save the new object to the database
acTrans.Commit();
// Dispose of the transaction
}
}
VBA/ActiveX 代码参考Sub RedefiningABlock()
' Define the block
Dim blockObj As AcadBlock
Dim insertionPnt(0 To 2) As Double
insertionPnt(0) = 0
insertionPnt(1) = 0
insertionPnt(2) = 0
On Error Resume Next
Set blockObj = Blocks("CircleBlock")
Err.Clear
If blockObj Is Nothing Then
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.ActiveLayout.Block.InsertBlock(insertionPnt, "CircleBlock", 1, 1, 1, 0)
Else
Set blockObj = ThisDrawing.Blocks.Item("CircleBlock")
Dim acObj As AcadObject
' Redefine the circle objects in the block
For Each acObj In blockObj
If TypeOf acObj Is AcadCircle Then
Dim acCirc As AcadCircle
Set acCirc = acObj
acCirc.radius = acCirc.radius * 2
End If
Next
' Update the blocks in the drawing
Regen acAllViewports
End If
End Sub
父主题: |
|Archiver|CAD开发者社区
( 苏ICP备2022047690号-1 苏公网安备32011402011833)
GMT+8, 2025-10-29 17:10
Powered by Discuz! X3.4
Copyright © 2001-2021, Tencent Cloud.