创建对象

在 AutoCAD 中经常有多种不同的方法可以创建相同的图形对象。虽然 .NET API 没有提供同样的创建对象的组合,但是它除为每一个对象类型都提供一个基本的对象构造函数外,也提供了对象构造函数的许多重载版本。

例如,在 AutoCAD 中创建圆有四种不同方法:

  • (1) 通过指定圆心和半径、
  • (2) 通过定义直径的两点、
  • (3) 通过定义圆周的三点或
  • (4) 通过两个切点和一个半径。
  • 但是,在 .NET API 中只提供了两种创建圆的方法。一个方法不接受任何参数,而第二个需要一个中心点,圆的法线方向和半径。

注意使用 New 关键字创建对象,然后使用 Add 或 AppendEntity 追加到父对象中,使用哪个方法要根据使用的是容器(符号表或字典) 还是块表记录对象。

为新对象指定默认属性值

当创建一个新图形对象后,应该调用新对象的 SetDatabaseDefaults 方法。 SetDatabaseDefaults 方法根据在当前文档的数据库中定义的当前图元的值设置下列各项图元的属性值。

确定父对象
创建直线
创建曲线对象
创建 Point 对象
创建实体填充区域
使用面域
创建图案填充

确定父对象

图形对象被添加到块表记录(BlockTableRecord)对象中,像模型或图纸空间。用户通过块表对象引用代表模型和图纸空间的块。如果想在当前空间代替在指定空间中工作,可以从当前数据库的 CurrentSpaceId 属性获得当前空间的 ObjectId。

模型或图纸空间的块表记录的 ObjectID 可以使用 DatabaseServices 命名空间下的 SymbolUtilityServices 类的属性或 GetBlockModelSpaceId 和 GetBlockPaperSpaceId 方法从块表对象中获得。

访问模型空间、图纸空间或当前空间

下列示例演示如何访问与模型空间、图纸空间或当前空间相关联的块表记录。一旦块表记录被引用,一个新的直线将被添加到块表记录中。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput<CommandMethod("AccessSpace")> _
Public Sub AccessSpace()'' 获得当前文档和数据库   Get the current document and databaseDim acDoc As Document = Application.DocumentManager.MdiActiveDocumentDim acCurDb As Database = acDoc.Database''启动一个事务   Start a transactionUsing acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()'' 以只读方式打开块表   Open the Block table for readDim acBlkTbl As BlockTableacBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)'' 以只读方式打开块表记录   Open the Block table record for readDim acBlkTblRec As BlockTableRecord'' 请求打开哪一个表记录  Request which table record to openDim pKeyOpts As PromptKeywordOptions = New PromptKeywordOptions("")pKeyOpts.Message = vbLf & "Enter which space to create the line in "pKeyOpts.Keywords.Add("Model")pKeyOpts.Keywords.Add("Paper")pKeyOpts.Keywords.Add("Current")pKeyOpts.AllowNone = FalsepKeyOpts.AppendKeywordsToMessage = TrueDim pKeyRes As PromptResult = acDoc.Editor.GetKeywords(pKeyOpts)If pKeyRes.StringResult = "Model" Then'' 从块表获得模型空间的 ObjectID    Get the ObjectID for Model space from the Block tableacBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _OpenMode.ForWrite)ElseIf pKeyRes.StringResult = "Paper" Then'' 从块表记录获得图纸空间的ObjectID   Get the ObjectID for Paper space from the Block tableacBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.PaperSpace), _OpenMode.ForWrite)Else'' 从数据库中获得当前空间的ObjectID    Get the ObjectID for the current space from the databaseacBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, _OpenMode.ForWrite)End If'' 创建一条起点为2,5 终点为 10,7 的直线    Create a line that starts at 2,5 and ends at 10,7Dim acLine As Line = New Line(New Point3d(2, 5, 0), _New Point3d(10, 7, 0))acLine.SetDatabaseDefaults()'' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transactionacBlkTblRec.AppendEntity(acLine)acTrans.AddNewlyCreatedDBObject(acLine, True)'' 保存新的直线到数据库中    Save the new line to the databaseacTrans.Commit()End Using
End Sub

创建直线

直线是 AutoCAD 中最基本的对象。用户可以创建各种直线—单一直线、带圆弧和不带圆弧的多线段。通常,可以通过指定坐标点来绘制直线。直线被创建后,它会从图形数据库继承当前设置,像图层、线型和颜色。

若要创建直线,可以创建下列任一个对象的新实例:

创建直线对象

本例添加一条起点为(5,5,0),终点为(12,3,0)的直线到模型空间。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry<CommandMethod("AddLine")> _
Public Sub AddLine()'' 获得当前文档和数据库   Get the current document and databaseDim acDoc As Document = Application.DocumentManager.MdiActiveDocumentDim acCurDb As Database = acDoc.Database''启动一个事务   Start a transactionUsing acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()'' 以只读方式打开块表   Open the Block table for readDim acBlkTbl As BlockTableacBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)'' 以写方式打开模型空间块表记录   Open the Block table record Model space for writeDim acBlkTblRec As BlockTableRecordacBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _OpenMode.ForWrite)'' 创建一条起点为(5,5,0),终点为(12,3,0)的直线  Create a line that starts at 5,5 and ends at 12,3Dim acLine As Line = New Line(New Point3d(5, 5, 0), _New Point3d(12, 3, 0))acLine.SetDatabaseDefaults()'' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transactionacBlkTblRec.AppendEntity(acLine)acTrans.AddNewlyCreatedDBObject(acLine, True)'' 保存新对象到数据库中   Save the new object to the databaseacTrans.Commit()End Using
End Sub

创建多段线对象

本例添加使用二维坐标(2,4)、(4,2) 和 (6,4)创建的一条有两个直线段的轻量多段线到模型空间中。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry<CommandMethod("AddLightweightPolyline")> _
Public Sub AddLightweightPolyline()'' 获得当前文档和数据库   Get the current document and databaseDim acDoc As Document = Application.DocumentManager.MdiActiveDocumentDim acCurDb As Database = acDoc.Database''启动一个事务   Start a transactionUsing acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()'' 以只读方式打开块表   Open the Block table for readDim acBlkTbl As BlockTableacBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)'' 以写方式打开模型空间块表记录   Open the Block table record Model space for writeDim acBlkTblRec As BlockTableRecordacBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _OpenMode.ForWrite)'' 创建一条有两段的多段线   Create a polyline with two segments (3 points)Dim acPoly As Polyline = New Polyline()acPoly.SetDatabaseDefaults()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 transactionacBlkTblRec.AppendEntity(acPoly)acTrans.AddNewlyCreatedDBObject(acPoly, True)'' 保存新对象到数据库中   Save the new object to the databaseacTrans.Commit()End Using
End Sub

创建曲线对象

用户可以使用 AutoCAD 创建各种曲线对象,包括样条曲线、螺旋线、圆、圆弧和椭圆。所有的曲线都在当前 UCS 的 XY 平面上创建。

若要创建曲线,可以创建下面任一个对象的新实例:

创建圆对象
创建圆弧对象
创建样条曲线对象

创建圆对象

本例在模型空间中创建一个中心点在 (2,3,0) ,半径为4.25 的圆。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry<CommandMethod("AddCircle")> _
Public Sub AddCircle()'' 获得当前文档和数据库   Get the current document and databaseDim acDoc As Document = Application.DocumentManager.MdiActiveDocumentDim acCurDb As Database = acDoc.Database''启动一个事务   Start a transactionUsing acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()'' 以只读方式打开块表   Open the Block table for readDim acBlkTbl As BlockTableacBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)'' 以写方式打开模型空间块表记录   Open the Block table record Model space for writeDim acBlkTblRec As BlockTableRecordacBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _OpenMode.ForWrite)'' 创建一个中心点在 (2,3,0) ,半径为4.25 的圆  Create a circle that is at 2,3 with a radius of 4.25Dim acCirc As Circle = New Circle()acCirc.SetDatabaseDefaults()acCirc.Center = New Point3d(2, 3, 0)acCirc.Radius = 4.25'' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transactionacBlkTblRec.AppendEntity(acCirc)acTrans.AddNewlyCreatedDBObject(acCirc, True)'' 保存新对象到数据库中   Save the new object to the databaseacTrans.Commit()End Using
End Sub

创建圆弧对象

本例在模型空间中创建一个中心点在 (6.25,9.125,0),半径为6,起始角度为1.117(64度),终点角度为3.5605(204度)的圆弧。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry<CommandMethod("AddArc")> _
Public Sub AddArc()'' 获得当前文档和数据库   Get the current document and databaseDim acDoc As Document = Application.DocumentManager.MdiActiveDocumentDim acCurDb As Database = acDoc.Database''启动一个事务   Start a transactionUsing acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()'' 以只读方式打开块表   Open the Block table for readDim acBlkTbl As BlockTableacBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)'' 以写方式打开模型空间块表记录   Open the Block table record Model space for writeDim acBlkTblRec As BlockTableRecordacBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _OpenMode.ForWrite)'' 创建一个中心点在 (6.25,9.125,0),半径为6,起始角度为1.117(64度),终点角度为3.5605(204度)的圆弧。   Create an arc that is at 6.25,9.125 with a radius of 6, and'' starts at 64 degrees and ends at 204 degreesDim acArc As Arc = New Arc(New Point3d(6.25, 9.125, 0), _6, 1.117, 3.5605)acArc.SetDatabaseDefaults()'' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transactionacBlkTblRec.AppendEntity(acArc)acTrans.AddNewlyCreatedDBObject(acArc, True)'' 保存新对象到数据库中   Save the new object to the databaseacTrans.Commit()End Using
End Sub

创建样条曲线对象

本例在模型空间中创建一个使用三个点 (0, 0, 0), (5, 5, 0), 和 (10, 0, 0)创建的圆。样条曲线的起始和终止切向为(0.5, 0.5, 0.0)。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry<CommandMethod("AddSpline")> _
Public Sub AddSpline()'' 获得当前文档和数据库   Get the current document and databaseDim acDoc As Document = Application.DocumentManager.MdiActiveDocumentDim acCurDb As Database = acDoc.Database''启动一个事务   Start a transactionUsing acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()'' 以只读方式打开块表   Open the Block table for readDim acBlkTbl As BlockTableacBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)'' 以写方式打开模型空间块表记录   Open the Block table record Model space for writeDim acBlkTblRec As BlockTableRecordacBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _OpenMode.ForWrite)'' 定义样条曲线的固定点   Define the fit points for the splineDim ptColl As Point3dCollection = New Point3dCollection()ptColl.Add(New Point3d(0, 0, 0))ptColl.Add(New Point3d(5, 5, 0))ptColl.Add(New Point3d(10, 0, 0))''从点(0.5,0.5,0)获得三维矢量    Get a 3D vector from the point (0.5,0.5,0)Dim vecTan As Vector3d = New Point3d(0.5, 0.5, 0).GetAsVector'' Create a spline through (0, 0, 0), (5, 5, 0), and (10, 0, 0) with a'' start and end tangency of (0.5, 0.5, 0.0)Dim acSpline As Spline = New Spline(ptColl, vecTan, vecTan, 4, 0.0)acSpline.SetDatabaseDefaults()'' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transactionacBlkTblRec.AppendEntity(acSpline)acTrans.AddNewlyCreatedDBObject(acSpline, True)'' 保存新对象到数据库中   Save the new object to the databaseacTrans.Commit()End Using
End Sub

创建 Point 对象

Point 对象非常有用,例如,可将 Point 对象用作捕捉的节点或者偏移对象的参考点。可以相对于屏幕或采用绝对单位来设置点的样式和大小。

数据库对象的 Pdmode 和 Pdsize 属性控制 Point 对象的外观。PDMODE 值 0、2、3 和 4 指定要通过点绘制的图形。值为 1 时表示不显示任何图形。

在上述值上加上 32、64 或 96 表示除了绘制通过点的图形以外,还在点的周围绘制形状:

PDSIZE 控制点图形的尺寸,PDMODE 值为 0 和 1 时除外。如果设置为 0,则点图形的高度是图形区高度的 5%。正的 PDSIZE 值指定点图形的绝对尺寸。负值将解释为视口大小的百分比。重生成图形时将重新计算所有点的大小。

更改 PDMODE 和 PDSIZE 后,现有点的外观将在下次重新生成图形时改变。

创建 Point 对象并更改其外观

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry<CommandMethod("AddPointAndSetPointStyle")> _
Public Sub AddPointAndSetPointStyle()'' 获得当前文档和数据库   Get the current document and databaseDim acDoc As Document = Application.DocumentManager.MdiActiveDocumentDim acCurDb As Database = acDoc.Database''启动一个事务   Start a transactionUsing acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()'' 以只读方式打开块表   Open the Block table for readDim acBlkTbl As BlockTableacBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)'' 以写方式打开模型空间块表记录   Open the Block table record Model space for writeDim acBlkTblRec As BlockTableRecordacBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _OpenMode.ForWrite)'' 在模型空间中创建一个坐标为(4,3,0)的点   Create a point at (4, 3, 0) in Model spaceDim acPoint As DBPoint = New DBPoint(New Point3d(4, 3, 0))acPoint.SetDatabaseDefaults()'' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transactionacBlkTblRec.AppendEntity(acPoint)acTrans.AddNewlyCreatedDBObject(acPoint, True)'' 在图形中设置所有点对象的样式   Set the style for all point objects in the drawingacCurDb.Pdmode = 34acCurDb.Pdsize = 1'' 保存新对象到数据库中   Save the new object to the databaseacTrans.Commit()End Using
End Sub

创建实体填充区域

用户可以创建用某种颜色填充的三角形和四边形的区域。要更快地得到结果,请在关闭 FILLMODE 系统变量时创建这些区域,然后再打开 FILLMODE 以填充完成的区域。

创建四边形实体填充区域时,第三点和第四点的次序将决定其形状。

前两点定义了多边形的一条边。第三点定义在第二点的对角处。如果第四点设置为等于第三点,则会创建一个填充三角形。

创建一个实体填充对象

下面示例使用坐标 (0,0,0)、(5,0,0)、(5,8,0) 和 (0,8,0) 在模型空间中创建四边形实体(蝴蝶结)。它也使用坐标(10, 0, 0), (15, 0, 0), (10, 8, 0)和 (15, 8, 0)创建一个矩形形状的四边形实体。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry<CommandMethod("Add2DSolid")> _
Public Sub Add2DSolid()'' 获得当前文档和数据库   Get the current document and databaseDim acDoc As Document = Application.DocumentManager.MdiActiveDocumentDim acCurDb As Database = acDoc.Database''启动一个事务   Start a transactionUsing acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()'' 以只读方式打开块表   Open the Block table for readDim acBlkTbl As BlockTableacBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)'' 以写方式打开模型空间块表记录   Open the Block table record Model space for writeDim acBlkTblRec As BlockTableRecordacBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _OpenMode.ForWrite)'' Create a quadrilateral (bow-tie) solid in Model spaceDim ac2DSolidBow As Solid = New Solid(New Point3d(0, 0, 0), _New Point3d(5, 0, 0), _New Point3d(5, 8, 0), _New Point3d(0, 8, 0))ac2DSolidBow.SetDatabaseDefaults()'' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transactionacBlkTblRec.AppendEntity(ac2DSolidBow)acTrans.AddNewlyCreatedDBObject(ac2DSolidBow, True)'' Create a quadrilateral (square) solid in Model spaceDim ac2DSolidSqr As Solid = New Solid(New Point3d(10, 0, 0), _New Point3d(15, 0, 0), _New Point3d(10, 8, 0), _New Point3d(15, 8, 0))ac2DSolidSqr.SetDatabaseDefaults()'' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transactionacBlkTblRec.AppendEntity(ac2DSolidSqr)acTrans.AddNewlyCreatedDBObject(ac2DSolidSqr, True)'' 保存新对象到数据库中   Save the new object to the databaseacTrans.Commit()End Using
End Sub

使用面域

面域是用户从称为环的闭合形状创建的二维闭合区域。环是由直线和不自交的曲线组成的封闭边界。环可以是直线、轻量优化多段线、2维和3维多段线,圆、圆弧、椭圆、椭圆弧、样条曲线、三维面、宽线和实体的组合。

组成环的对象必须是闭合的,或者是通过与其他对象共享端点而形成闭合的区域。所有这些对象还必须共面(在同一个平面上)。组成面域的环必须定义为对象的数组。

创建面域

面域是被添加到块表记录对象中的,它是通过创建一个面域的实例然后把它追加到块表记录的。在将面域添加到块表记录中前,需要根据对象创建一个封闭环。CreateFromCurves 函数通过输入的对象数组组成的每一个封闭环创建面域。CreateFromCurves 方法返回和需要一个 DBObjectCollection 对象。

AutoCAD 将闭合的二维和平面三维多段线转换为独立的面域,然后转换形成闭合平面环的多段线、直线和曲线。如果有两条以上的曲线共用一个端点,得到的面域可能是不确定的。所以,在使用 CreateFromCurves 方法时,实际可能会创建多个面域。用户需要追加每一个创建的面域到块表记录对象中去。

创建一个简单的面域

本例创建从一个简单的圆中创建一个面域。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry<CommandMethod("AddRegion")> _
Public Sub AddRegion()'' 获得当前文档和数据库   Get the current document and databaseDim acDoc As Document = Application.DocumentManager.MdiActiveDocumentDim acCurDb As Database = acDoc.Database''启动一个事务   Start a transactionUsing acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()'' 以只读方式打开块表   Open the Block table for readDim acBlkTbl As BlockTableacBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)'' 以写方式打开模型空间块表记录   Open the Block table record Model space for writeDim acBlkTblRec As BlockTableRecordacBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _OpenMode.ForWrite)'' 在内存中创建圆  Create an in memory circleUsing acCirc As Circle = New Circle()acCirc.SetDatabaseDefaults()acCirc.Center = New Point3d(2, 2, 0)acCirc.Radius = 5'' 添加圆到对象数组中   Adds the circle to an object arrayDim acDBObjColl As DBObjectCollection = New DBObjectCollection()acDBObjColl.Add(acCirc)'' 根据每一个闭合环计算面域   Calculate the regions based on each closed loopDim myRegionColl As DBObjectCollection = New DBObjectCollection()myRegionColl = Region.CreateFromCurves(acDBObjColl)Dim acRegion As Region = myRegionColl(0)'' 添加新对象到块表记录和事务中   Add the new object to the block table record and the transactionacBlkTblRec.AppendEntity(acRegion)acTrans.AddNewlyCreatedDBObject(acRegion, True)'' Dispose of the in memory object not appended to the databaseEnd Using'' 保存新对象到数据库中   Save the new object to the databaseacTrans.Commit()End Using
End Sub

创建组合面域

可以通过查找面域或三维实体的差集、并集或交集来创建组合面域。然后可以拉伸或旋转组合面域以创建复杂的实体。要创建组合面域,请使用 BooleanOperation 方法。

减去面域

当从某个面域减去另一个面域时,需要从第一个面域调用 BooleanOperation 方法。这是要从中减去其他面域的的面域。例如,要计算地板需要铺多少地毯,需要从地板空间的外部边界调用 BooleanOperation 方法,并将不需要铺地毯的区域如柱子和柜台作为 Boolean 参数列表中的对象。

并集面域

要获得并集面域,可以调用 BooleanOperation 方法并在操作中输入常量 用户可以按任意次序组合面域来形成并集面域。

查找两个面域的交集

要查找两个面域的交集,请使用常量 BooleanOperationType.BoolIntersect。可以按任意次序组合面域来计算这些面域的交集。

创建组合面域

本例从两个圆创建两个面域,然后用大的面域减去小的面域以创建一个轮形图。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry<CommandMethod("CreateCompositeRegions")> _
Public Sub CreateCompositeRegions()'' 获得当前文档和数据库   Get the current document and databaseDim acDoc As Document = Application.DocumentManager.MdiActiveDocumentDim acCurDb As Database = acDoc.Database''启动一个事务   Start a transactionUsing acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()'' 以只读方式打开块表   Open the Block table for readDim acBlkTbl As BlockTableacBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)'' 以写方式打开模型空间块表记录   Open the Block table record Model space for writeDim acBlkTblRec As BlockTableRecordacBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _OpenMode.ForWrite)'' 在内存中创建两个圆   Create two in memory circlesDim acCirc1 As Circle = New Circle()acCirc1.SetDatabaseDefaults()acCirc1.Center = New Point3d(4, 4, 0)acCirc1.Radius = 2Dim acCirc2 As Circle = New Circle()acCirc2.SetDatabaseDefaults()acCirc2.Center = New Point3d(4, 4, 0)acCirc2.Radius = 1'' 添加圆到对象数组中   Adds the circle to an object arrayDim acDBObjColl As DBObjectCollection = New DBObjectCollection()acDBObjColl.Add(acCirc1)acDBObjColl.Add(acCirc2)'' 根据每一个闭合环计算面域   Calculate the regions based on each closed loopDim myRegionColl As DBObjectCollection = New DBObjectCollection()myRegionColl = Region.CreateFromCurves(acDBObjColl)Dim acRegion1 As Region = myRegionColl(0)Dim acRegion2 As Region = myRegionColl(1)'' 从面域2减去面域1   Subtract region 1 from region 2If acRegion1.Area > acRegion2.Area Then'' 从大的一个中减去小的面域   Subtract the smaller region from the larger oneacRegion1.BooleanOperation(BooleanOperationType.BoolSubtract, acRegion2)acRegion2.Dispose()'' 添加遇终的面域到数据库中   Add the final region to the databaseacBlkTblRec.AppendEntity(acRegion1)acTrans.AddNewlyCreatedDBObject(acRegion1, True)Else'' 从大的一个中减去小的面域   Subtract the smaller region from the larger oneacRegion2.BooleanOperation(BooleanOperationType.BoolSubtract, acRegion1)acRegion1.Dispose()'' 添加遇终的面域到数据库中   Add the final region to the databaseacBlkTblRec.AppendEntity(acRegion2)acTrans.AddNewlyCreatedDBObject(acRegion2, True)End If'' 不追加对象到数据库中直接从内存中销毁    Dispose of the in memory objects not appended to the databaseacCirc1.Dispose()acCirc2.Dispose()'' 保存新对象到数据库中   Save the new object to the databaseacTrans.Commit()End Using
End Sub

创建图案填充

图案填充是可以使用某种图案来填充图形的封闭边界。

在创建图案填充时,不是在开始的时候指定要填充的区域。首先必须创建 Hatch 对象。完成后,可以指定外部环,这是图案填充最外面的边界。然后继续指定图案填充中可能存在的所有内部环。

创建 Hatch 对象

创建 Hatch 对象时,需要指定填充图案类型、填充图案名和关联性。一旦创建 Hatch 对象,就无法再更改其关联性。

要创建 Hatch 对象,请创建一个新的对象实例,然后使用 AppendEntity 方法将其添加到块表记录对象中去。

关联图案填充

用户可以创建关联的或非关联的图案填充。关联的图案填充链接到它们的边界并且会在边界改变时自动更新,而非关联的图案填充则独立于它们的边界。

要使图案填充具有关联性,请将创建的图案填充对象的关联特性设置为TRUE。若要使图案填充不关联,请将“关联”特性设置为FALSE。

hatch 关联性必须在添加填充环前设置。如果 Hatch 对象是非关联性的,那么可以通过设置 Associative 属性为 TRUE 让其再次关联然后添加填充环。

指定填充图案的类型和名称

AutoCAD 提供了实体填充的五十多种行业标准填充图案。填充图案可用于亮显图形的特定特征或区域。例如,图案可以帮助区分三维对象的组成部分,或者表示组成对象的材质。

可以使用 AutoCAD 附带的图案或外部图案库中的图案。

要指定唯一的图案,用户必须在创建 Hatch 对象时输入图案的类型和名称。图案类型指定在何处查找图案名称。输入图案类型时,可以使用以下常量:

HatchPatternType.PreDefined

从 acad.pat 文件中定义的图案名中进行选择。

HatchPatternType.UserDefined

用当前线型定义直线图案。

HatchPatternType.CustomDefined

从 PAT 而不是 acad.pat 文件中选择图案名。

输入图案名称时,名称必须对由图案类型指定的文件有效。

定义填充边界

一旦 Hatch 对象被创建,就可以添加图案填充边界。边界可以是直线、圆弧、圆、二维多段线、椭圆、样条曲线和面域的任意组合。

添加的第一个边界必须是外边界,即用于定义图案填充最外面的边界。要添加外部边界,请使用添加环的类型为 HatchLoopTypes.Outermost 常量的 AppendLoop 方法。

一旦外边界被定义,就可以继续添加另外的边界。添加内部边界请使用带 HatchLoopTypes.Default 常量的 AppendLoop 方法。

内边界定义图案填充内的孤岛。Hatch 对象处理这些孤岛的方式取决于 HatchStyle 属性的设置。HatchStyle 属性可以设置为以下状态:

HatchStyle.Normal

指定标准的样式,即普通。此选项从最外面的区域边界向内进行图案填充。如果 AutoCAD 遇到内部边界,将停止填充,直到遇到另一个内部边界。这是 HatchStyle 属性的默认设置。

HatchStyle.Outer

仅填充最外面的区域。此样式也是从最外面的区域边界向内进行图案填充,但是遇到内部边界时会关闭图案填充并且不再打开。

HatchStyle.Ignore

忽略内部结构。此选项使图案填充通过所有的内部对象。

定义完图案填充之后,必须先对其进行计算,然后才能显示。请使用 EvaluateHatch 方法完成此任务。

创建 Hatch 对象

本例在模型空间中创建关联的图案填充。创建图案填充后,可以修改与图案填充关联的圆的大小。图案填充将自动改变以匹配圆的当前大小。

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry<CommandMethod("AddHatch")> _
Public Sub AddHatch()'' 获得当前文档和数据库   Get the current document and databaseDim acDoc As Document = Application.DocumentManager.MdiActiveDocumentDim acCurDb As Database = acDoc.Database''启动一个事务   Start a transactionUsing acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()'' 以只读方式打开块表   Open the Block table for readDim acBlkTbl As BlockTableacBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)'' 以写方式打开模型空间块表记录   Open the Block table record Model space for writeDim acBlkTblRec As BlockTableRecordacBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _OpenMode.ForWrite)'' 创建一个圆对象作为图案填充的封闭边界   Create a circle object for the closed boundary to hatchDim acCirc As Circle = New Circle()acCirc.SetDatabaseDefaults()acCirc.Center = New Point3d(3, 3, 0)acCirc.Radius = 1'' 添加新的圆对象到块表记录和事务中   Add the new circle object to the block table record and the transactionacBlkTblRec.AppendEntity(acCirc)acTrans.AddNewlyCreatedDBObject(acCirc, True)'' 添加圆到一个 ObjectID 数组中去    Adds the circle to an object id arrayDim acObjIdColl As ObjectIdCollection = New ObjectIdCollection()acObjIdColl.Add(acCirc.ObjectId)'' 创建图案填充对象并添加到块表记录中    Create the hatch object and append it to the block table recordDim acHatch As Hatch = New Hatch()acBlkTblRec.AppendEntity(acHatch)acTrans.AddNewlyCreatedDBObject(acHatch, True)'' Set the properties of the hatch object'' Associative must be set after the hatch object is appended to the '' block table record and before AppendLoopacHatch.SetDatabaseDefaults()acHatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31")acHatch.Associative = TrueacHatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl)acHatch.EvaluateHatch(True)'' 保存新对象到数据库中   Save the new object to the databaseacTrans.Commit()End Using
End Sub

autoCAD 创建对象 使用面域 创建图案填充相关推荐

  1. html画布360图案填充_在Photoshop中创建带有图案的抽象设计

    效果图 知识点: 应用选区工具结合图层混合模式和混合选项,创造出唯美的抽象类画册 效果 设计本身由一系列同心圆组成,每组的大小各不相同.每个圆都有6个核心色板的底色,然后渐变和图案填充会增加细节和深度 ...

  2. autocad.net计算图案填充的周长

    //参考:https://adndevblog.typepad.com/autocad/2012/04/perimeter-of-a-hatch-using-objectarx-and-autocad ...

  3. java 填充pdf_Java如何创建和填充PDF表单域(代码示例)

    本篇文章给大家带来的内容是关于Java如何创建和填充PDF表单域(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 表单域,可以按用途分为多种不同的类型,常见的有文本框.多行 ...

  4. Java 创建、填充PDF表单域

    表单域,可以按用途分为多种不同的类型,常见的有文本框.多行文本框.密码框.隐藏域.复选框.单选框和下拉选择框等,目的是用于采集用户的输入或选择的数据.下面的示例中,将分享通过Java编程在PDF中添加 ...

  5. java 填充pdf_Java创建和填充PDF表单域方法

    表单域,可以按用途分为多种不同的类型,常见的有文本框.多行文本框.密码框.隐藏域.复选框.单选框和下拉选择框等,目的是用于采集用户的输入或选择的数据.下面的示例中,将分享通过Java编程在PDF中添加 ...

  6. 用计算机在记事本做图案,记事本如何自定义制作CAD图案填充

    使用记事本通过对CAD填充图案文件进行编辑,添加新的图案填充. 1.*PAT库文件标准格式: *pattern-name [, description] angle, x-origin, y-orig ...

  7. CAD填充技巧:1分钟带你摸透CAD图案填充!

    在使用浩辰CAD软件绘制CAD图纸的过程中,图案填充功能是CAD设计师们的常用功能之一,用于绘制各种CAD图纸.在浩辰CAD软件中,CAD图案填充功能的参数设置其实非常丰富.接下来给大家介绍一些比较实 ...

  8. CAD图案填充:什么是CAD线图案?

    在使用国产CAD软件绘制建筑CAD图纸的过程中,经常会用到CAD图案填充功能,那么在浩辰CAD建筑软件中如何使用线图案填充呢?接下来的CAD教程就和小编一起来看看国产CAD软件--浩辰CAD建筑软件中 ...

  9. PPT怎么插入图案填充效果

    转载者: ppt素材库              来源: www.2ppt.cn 大家在使用office2010做PPT的时候不知道office2010怎么在PPT图表的插入及图表图案填充效果,其实方 ...

最新文章

  1. c++ decltype
  2. 锁相环环路滤波器计算公式_锁相环计算方法
  3. 3.Android 优化布局(解决TextView布局)
  4. mysql5.6安装51cto_MySQL 5.6 for Windows配置安装之解压缩版
  5. 深入理解分布式系统的2PC和3PC
  6. webservice生成客户端的方法
  7. from PyQt4 import QtGui,QtCore出错-解
  8. 软件工程资料 - 优秀的大学怎么教程序开发和软件工程课
  9. 寒假作业3(传说中的写软件)
  10. windows下protobuf jar包的编译
  11. 计算机课件白板培训,希沃白板使用教学,直接导入PPT课件并修改
  12. ubuntu20.04安装微信
  13. 电器上的这些符号有什么特别含义?
  14. 征集国内操作系统项目列表
  15. XM 玻璃钢一体化泵站特点及使用寿命
  16. 最新交易猫源码+独立后台管理
  17. 【数据处理】格式化数据
  18. 遇到人生低谷期该怎么度过?
  19. 实验三+163+张玉洁
  20. 企业信息管理系统(4)_用户查询

热门文章

  1. 创业如创作,保持热爱
  2. 操作系统课程设计--模拟时间片轮转法
  3. tmall.product.schema.add(淘宝天猫商品发布接口),淘宝商品发布API接口,tmall.product.schema.get产品信息获取接口
  4. ASP.NET MVC 音乐商店 - 4. 数据访问
  5. 硬件知识:内存条出现故障的解决方案!
  6. android 多任务按钮,XDA大神推出Android多任务切换神器
  7. Win11新加硬盘不显示的解决方法
  8. 【vue】纯前端登录验证码实现记录
  9. STM32遇到的坑!你知道几个?
  10. cpda和cda区别与联系