[资料] 创建和编辑AutoCAD实体(四) 编辑二维命名对象 (5)
7、Extend and Trim Objects延伸和修剪对象You can change the angle of arcs and you can change the length of lines, open polylines, elliptical arcs, and open splines. The results are similar to both extending and trimming objects.我们可以改变圆弧的角度,还可以改变直线、开放多段线、椭圆弧及开放样条曲线的长度。其结果类似于延伸对象和修剪对象。You can extend or trim an object by editing its properties. For example, to lengthen a line, simply change the coordinates of the StartPoint or EndPoint properties. To change the angle of an arc, change the StartAngle or EndAngle property of the arc. Once you have altered an object's property or properties, you might need to regenerate the display to see the changes in the drawing window.我们可以编辑对象属性来延伸或修剪对象。例如,延长一条直线,只需简单修改直线的StartPoint属性或EndPoint属性坐标。修改圆弧的角度,只需简单修改圆弧的StartAngle 属性或EndAngle属性。一旦改变了对象的一个或多个属性,我们就需要重新生成图形显示才能看到所作的修改。For more information about extending and trimming objects, see “Resize or Reshape Objects” in theAutoCAD User's Guide.更多关于延伸和修剪对象的内容,见AutoCAD用户指南中的“修改对象的大小和形状”内容。Lengthen a line延长一条直线This example creates a line and then changes the endpoint of that line, resulting in a longer line.本例创建一条直线,然后修改其EndPoint属性来使直线变长。VB.NETImports Autodesk.AutoCAD.Runtime<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><strong></strong></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Imports Autodesk.AutoCAD.ApplicationServices</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Imports Autodesk.AutoCAD.DatabaseServices</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Imports Autodesk.AutoCAD.Geometry</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><CommandMethod("ExtendObject")> _</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Public Sub ExtendObject()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">'' Get the current document and database</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Dim acCurDb As Database = acDoc.Database</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">'' Start a transaction</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Open the Block table for read</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim acBlkTbl As BlockTable</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> OpenMode.ForRead)</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Open the Block table record Model space for write</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim acBlkTblRec As BlockTableRecord</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> OpenMode.ForWrite)</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Create a line that starts at (4,4,0) and ends at (7,7,0)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim acLine As Line = New Line(New Point3d(4, 4, 0), _</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> New Point3d(7, 7, 0))</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Add the new object to the block table record and the transaction</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acBlkTblRec.AppendEntity(acLine)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acTrans.AddNewlyCreatedDBObject(acLine, True)</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Update the display and diaplay a message box</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acDoc.Editor.Regen()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Application.ShowAlertDialog("Before extend")</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Double the length of the line</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acLine.EndPoint = acLine.EndPoint + acLine.Delta</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> '' Save the new object to the database</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acTrans.Commit()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">End Using</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">End Sub</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><strong>C#</strong></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.Runtime;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.ApplicationServices;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.DatabaseServices;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using Autodesk.AutoCAD.Geometry;</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">public static void ExtendObject()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">{</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">// Get the current document and database</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Document acDoc = Application.DocumentManager.MdiActiveDocument;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Database acCurDb = acDoc.Database;</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">// Start a transaction</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">{</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Open the Block table for read</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> BlockTable acBlkTbl;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> OpenMode.ForRead) as BlockTable;</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Open the Block table record Model space for write</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> BlockTableRecord acBlkTblRec;</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acBlkTblRec = acTrans.GetObject(acBlkTbl,</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> OpenMode.ForWrite) as BlockTableRecord;</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Create a line that starts at (4,4,0) and ends at (7,7,0)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Line acLine = new Line(new Point3d(4, 4, 0),</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> new Point3d(7, 7, 0));</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Add the new object to the block table record and the transaction</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acBlkTblRec.AppendEntity(acLine);</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acTrans.AddNewlyCreatedDBObject(acLine, true);</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Update the display and diaplay a message box更新显示</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acDoc.Editor.Regen();</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Application.ShowAlertDialog("Before extend");</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Double the length of the line直线延长一倍</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acLine.EndPoint = acLine.EndPoint + acLine.Delta;</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> // Save the new object to the database保存新对象到数据库</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> acTrans.Commit();</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">}</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">}</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "><strong>VBA/ActiveX Code Reference</strong></div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">Sub ExtendObject()</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' Define and create the line</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim lineObj As AcadLine</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim startPoint(0 To 2) As Double</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Dim endPoint(0 To 2)As Double</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> startPoint(0) = 4</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> startPoint(1) = 4</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> startPoint(2) = 0</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> endPoint(0) = 7</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> endPoint(1) = 7</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> endPoint(2) = 0</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> Set lineObj = ThisDrawing.ModelSpace. _</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> AddLine(startPoint, endPoint)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> lineObj.Update</div>
<div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> ' Double the length of the line</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> endPoint(0) = lineObj.endPoint(0) + lineObj.Delta(0)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> endPoint(1) = lineObj.endPoint(1) + lineObj.Delta(1)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> endPoint(2) = lineObj.endPoint(2) + lineObj.Delta(2)</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> lineObj.endPoint = endPoint</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; "> lineObj.Update</div><div align="left" style="color: rgb(51, 51, 51); font-family: Arial; ">End Sub</div>
页:
[1]