您的位置:首页 > 其它

03-04 创建和编辑AutoCAD实体(四) 编辑二维命名对象 (1)

2011-08-02 18:15 387 查看
Edit Named and 2D Objects 编辑二维命名对象
Existing objects can be modified with the methods and properties associated with each object. If you modify a visible property of a graphic object, use the Regen method to redraw the object on screen. The Regen method is a member of the Editor
object.
可以使用与对象关联的方法和属性对现有对象进行修改。如果修改了图形对象的可见性属性,记着用Regen方法重画屏幕上的对象。Regen方法是Editor对象的一个成员。

Topics in this section
本节内容
· Work with Named Objects 使用命名对象
· Erase Objects 删除对象
· Copy Objects 复制对象
· Offset Objects 偏移对象
· Transform Objects 变换对象
· Array Objects 阵列对象
· Extend and Trim Objects 延伸和修剪对象
· Explode Objects 分解对象
· Edit Polylines 编辑多段线
· Edit Splines 编辑样条曲线
· Edit Hatches 编辑图案填充

1、Work with Named Objects使用命名对象

In addition to the graphical objects used by AutoCAD, there are several types of nongraphical objects stored in a drawing database. These objects have descriptive designations associated with them. For example, blocks, layers, groups, and dimension
styles all have a name assigned to them and in most cases can be renamed. The names of symbol table records are displayed in the user interface of AutoCAD, while the object id of an object is used to reference the object in most cases throughout the .NET API.
AutoCAD除了使用图形对象外,还有许多非图形对象类型存储在图形数据库中。这些对象都有与之相关联的描述性名称。例如,块、图层、编组以及标注样式等,所有这些对象都被赋予一个名称,并且多数情况下可以对其重命名。符号表记录的名称直接显示在AutoCAD用户界面上,而多数情况下通过.NET API编程引用对象时使用的是对象的标识Id。
For example, the object id of a LayerTableRecord object is assigned to a graphical object’s Layer property and not the actual name that is assigned to the LayerTableRecord. However, the object id of a LayerTableRecord object can be obtained
from the Layer table using the name of the layer you want to access.
例如,某图形对象的图层属性获得的赋值是LayerTableRecord对象的标识id,而不是LayerTableRecord对象的实际名称。不过,LayerTableRecord对象的标识id可以用所访问图层的名称从Layer表获得。

Topics in this section
本小节内容
· Purge Unreferenced Named Objects 清理未引用的命名对象
· Rename Objects 重命名对象

1.1、Purge Unreferenced Named Objects清理未引用的命名对象

Unreferenced named objects can be purged from a database at any time. You cannot purge named objects that are referenced by other objects. For example, a font file might be referenced by a text style or a layer might be referenced by the objects
on that layer. Purging reduces the size of a drawing file when saved to disk.
可以随时将未引用的对象从数据库中清理出去。不可以清理被其他对象引用的对象。例如,某字体文件可能正由文字样式引用,或者,某图层正由该层上的对象引用。清理工作可以减小图形文件存盘时的大小。
Unreferenced objects are purged from a drawing database with the Purge method. The Purge method requires a list of objects you want to purge in the form of an ObjectIdCollection or ObjectIdGraph objects. The ObjectIdCollection or ObjectIdGraph
objects passed into the Purge method are updated with the objects in which can be erased from the database. After the call to Purge, you must erase each individual object returned.
使用Puege方法从图形数据库中清除未引用对象。Purge方法需要一个包含要清除的对象的列表,列表为ObjectIdCollection对象或ObjectIdGraph对象。传入Purge方法的ObjectIdCollection对象或ObjectIdGraph对象会更新为只包含要从数据库中删除的对象。调用Purge方法后,必须显式地删除返回的每个对象。
VBA/ActiveX Cross Reference VBA/ActiveX交叉参考
In the ActiveX Automation library you would use the PurgeAll method to remove all unreferenced named objects and it would identify which objects can be removed. However, with the .NET API you need to supply which objects you would like to purge
and then the Purge method returns to you which ones actually can be. So there is a bit more work involved when working with the .NET API to purge all unreferenced named objects from a database.
在ActiveX Automation库中,我们使用PurgeAll方法删除所有未被引用的命名对象,而且该方法能够识别哪个对象可以删除。可是,使用.NET API我们需提供要清除的对象给Purge方法,然后Purge方法会返回实际可以删除的对象。可见,使用.NET API从数据库中清除所有未引用的命名对象时,相对繁琐一些。
ThisDrawing.PurgeAll

Purge all unreferenced layers 清除所有未引用的图层

The following example demonstrates how to purge all unreferenced layers from a database.
下面这个例子演示怎样从数据库清除所有未引用的图层。
VB.NET
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices

<CommandMethod("PurgeUnreferencedLayers")> _
Public Sub PurgeUnreferencedLayers()
'' Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database

'' Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

'' Open the Layer table for read
Dim acLyrTbl As LayerTable
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _
OpenMode.ForRead)

'' Create an ObjectIdCollection to hold the object ids for each table record
Dim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()

'' Step through each layer and add it to the ObjectIdCollection
For Each acObjId As ObjectId In acLyrTbl
acObjIdColl.Add(acObjId)
Next

'' Remove the layers that are in use and return the ones that can be erased
acCurDb.Purge(acObjIdColl)

'' Step through the returned ObjectIdCollection
For Each acObjId As ObjectId In acObjIdColl
Dim acSymTblRec As SymbolTableRecord
acSymTblRec = acTrans.GetObject(acObjId, _
OpenMode.ForWrite)

Try
'' Erase the unreferenced layer
acSymTblRec.Erase(True)
Catch Ex As Autodesk.AutoCAD.Runtime.Exception
'' Layer could not be deleted
Application.ShowAlertDialog("Error:" & vbLf & Ex.Message)
End Try
Next

'' Commit the changes and dispose of the transaction
acTrans.Commit()
End Using
End Sub
C#
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;

[CommandMethod("PurgeUnreferencedLayers")]
public static void PurgeUnreferencedLayers()
{
// Get the current document and database获取当前文档数据库
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

// Start a transaction启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Layer table for read以读打开Layer表
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForRead) as LayerTable;

// Create an ObjectIdCollection to hold the object ids for each table record
// 创建ObjectIdCollection用来存放每条表记录的对象id;
ObjectIdCollection acObjIdColl = new ObjectIdCollection();

// Step through each layer and add iterator to the ObjectIdCollection
// 遍历图层并将图层id添加到ObjectIdCollection
foreach (ObjectId acObjId in acLyrTbl)
{
acObjIdColl.Add(acObjId);
}

// Remove the layers that are in use and return the ones that can be erased
// 调用Purge方法:传入参数 - ObjectIdCollection对象;
// 返回 – 更新了的ObjectIdCollection对象,包含可以删除的图层;
acCurDb.Purge(acObjIdColl);

// Step through the returned ObjectIdCollection
// and erase each unreferenced layer
// 遍历返回的ObjectIdCollection对象,并删除未引用的图层;
foreach (ObjectId acObjId in acObjIdColl)
{
SymbolTableRecord acSymTblRec;
acSymTblRec = acTrans.GetObject(acObjId,
OpenMode.ForWrite) as SymbolTableRecord;

try
{
// Erase the unreferenced layer删除未引用的图层
acSymTblRec.Erase(true);
}
catch (Autodesk.AutoCAD.Runtime.Exception Ex)
{
// Layer could not be deleted图层不能删除
Application.ShowAlertDialog("Error:\n" + Ex.Message);
}
}

// Commit the changes and dispose of the transaction
// 提交修改,关闭事务
acTrans.Commit();
}
}

1.2、Rename Objects重命名对象

As your drawings become more complex, you can rename objects to keep the names meaningful or to avoid conflicts with names in other drawings you have inserted or attached. The Name property is used to get the current name or change the name
of a named object.
随着图形越来越复杂,我们可以给对象重新命名,以使对象名称有意义,或避免与所插入或附加的其他图形中的对象名发生冲突。Name属性用来获取命名对象的当前名称,也可用来修改命名对象的名称。
You can rename any named object except those reserved by AutoCAD, for example, layer 0 or the CONTINUOUS linetype.
我们可以对任何命名对象重命名,AutoCAD保留的除外,如图层0或CONTINUOUS线型。
Names can be up to 255 characters long. In addition to letters and numbers, names can contain spaces (although AutoCAD removes spaces that appear directly before and after a name) and any special character not used by Microsoft® Windows®
or AutoCAD for other purposes. Special characters that you cannot use include less-than and greater-than symbols (< >), forward slashes and backslashes (/ \), quotation marks ("), colons (:), semicolons (;), question marks (?), commas (,), asterisks (*), vertical
bars (|), equal signs (=), and single quotes ('). You also cannot use special characters created with Unicode fonts.
名称最长可以到255个字符,除字母和数字外,还可以包含空格(尽管AutoCAD会删除直接出现在名称前后的空格)以及Microsoft® Windows® 或AutoCAD没有使用的其他特殊字符。不能用于对象名称的特殊字符包括大于号和小于号(< >)、前后斜杠(/ \)、引号(”)、冒号(:)、分号(;)、问号(?)、逗号(,)、星号(*)、竖杠(|)、等号(=)及单引号(’)。Unicode字体创建的特殊字符也不能用。

Rename a layer 重命名图层

This example creates a copy of layer "0" and renames the new layer to “MyLayer”.
本例创建图层0的一个副本并命名为MyLayer。
VB.NET
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices

<CommandMethod("RenameLayer")> _
Public Sub RenameLayer()
'' Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database

'' Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

'' Returns the layer table for the current database
Dim acLyrTbl As LayerTable
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, _
OpenMode.ForWrite)

'' Clone layer 0 (copy it and its properties) as a new layer
Dim acLyrTblRec As LayerTableRecord
acLyrTblRec = acTrans.GetObject(acLyrTbl("0"), _
OpenMode.ForRead).Clone()

'' Change the name of the cloned layer
acLyrTblRec.Name = "MyLayer"

'' Add the cloned layer to the Layer table and transaction
acLyrTbl.Add(acLyrTblRec)
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, True)

'' Save changes and dispose of the transaction
acTrans.Commit()
End Using
End Sub
C#
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;

[CommandMethod("RenameLayer")]
public static void RenameLayer()
{
// Get the current document and database获取当前文档和数据库
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

// Start a transaction启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Returns the layer table for the current database
// 返回当前数据库的图层表
LayerTable acLyrTbl;
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId,
OpenMode.ForWrite) as LayerTable;

// Clone layer 0 (copy it and its properties) as a new layer
// 克隆图层0(复制其及其属性)
LayerTableRecord acLyrTblRec;
acLyrTblRec = acTrans.GetObject(acLyrTbl["0"],
OpenMode.ForRead).Clone() as LayerTableRecord;

// Change the name of the cloned layer
// 修改克隆得到的图层的名称
acLyrTblRec.Name = "MyLayer";

// Add the cloned layer to the Layer table and transaction
// 使用图层MyLayer可用
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);

// Save changes and dispose of the transaction
// 保存修改,关闭事务
acTrans.Commit();
}
}

2、Erase Objects删除对象

You can delete non-graphical and graphical objects with the Erase method.
我们可以使用Erase方法删除非图形对象及图形对象。
WarningWhile many non-graphical objects, such as the Layer table and Model space block table records have an Erase method, it should not be called. If Erase is called on one of these objects, an error will occur.
警告:尽管许多非图形对象,如Layer表及模型空间块表记录等,都拥有Erase方法,但不能对这些对象调用该方法,否则会发生错误。

Create and erase a polyline 创建一个多段线并删除它

This example creates a lightweight polyline, then erases it.
本例创建一个轻量级多段线,然后将它删除。
VB.NET
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry

<CommandMethod("EraseObject")> _
Public Sub EraseObject()
'' Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database

'' Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

'' Open the Block table for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

'' Open the Block table record Model space for write
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
OpenMode.ForWrite)

'' Create a lightweight polyline
Dim acPoly As Polyline = New Polyline()
acPoly.AddVertexAt(0, New Point2d(2, 4), 0, 0, 0)
acPoly.AddVertexAt(1, New Point2d(4, 2), 0, 0, 0)
acPoly.AddVertexAt(2, New Point2d(6, 4), 0, 0, 0)

'' Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acPoly)
acTrans.AddNewlyCreatedDBObject(acPoly, True)

'' Update the display and display an alert message
acDoc.Editor.Regen()
Application.ShowAlertDialog("Erase the newly added polyline.")

'' Erase the polyline from the drawing
acPoly.Erase(True)

'' Save the new object to the database
acTrans.Commit()
End Using
End Sub
C#
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;

[CommandMethod("EraseObject")]
public static void EraseObject()
{
// Get the current document and database获取当前文档和数据库
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;

// Start a transaction启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read以读打开块表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write
// 以写打开块表记录模型空间
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;

// Create a lightweight polyline创建轻量级多段线
Polyline acPoly = new Polyline();
acPoly.AddVertexAt(0, new Point2d(2, 4), 0, 0, 0);
acPoly.AddVertexAt(1, new Point2d(4, 2), 0, 0, 0);
acPoly.AddVertexAt(2, new Point2d(6, 4), 0, 0, 0);

// Add the new object to the block table record and the transaction
// 添加新对象到块表记录和事务
acBlkTblRec.AppendEntity(acPoly);
acTrans.AddNewlyCreatedDBObject(acPoly, true);

// Update the display and display an alert message
// 更新显示并显示一条告警消息
acDoc.Editor.Regen();
Application.ShowAlertDialog("Erase the newly added polyline.");

// Erase the polyline from the drawing
// 从图形中删除多段线
acPoly.Erase(true);

// Save the new object to the database
// 提交修改
acTrans.Commit();
}
}
VBA/ActiveX Code Reference
Sub EraseObject()
' Create the polyline
Dim lwpolyObj As AcadLWPolyline
Dim vertices(0 To 5) As Double
vertices(0) = 2: vertices(1) = 4
vertices(2) = 4: vertices(3) = 2
vertices(4) = 6: vertices(5) = 4
Set lwpolyObj = ThisDrawing.ModelSpace. _
AddLightWeightPolyline(vertices)
ZoomAll
' Erase the polyline
lwpolyObj.Delete
ThisDrawing.Regen acActiveViewport
End Sub
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐