Imports System.Drawing
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.ApplicationServices.DocumentExtension
Namespace sweeping
Public Class Testing
<CommandMethod("jointwolines")>
Public Shared Sub jointwolines()
Dim line1, line2 As Line
Dim pll As polyline
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim bt As BlockTable
Dim btr As BlockTableRecord
Using tr As Transaction = db.TransactionManager.StartTransaction()
line1 = New line(New point3d(0, 0, 0), New point3d(100, 0, 0))
line2 = New line(New point3d(100, 0, 0), New point3d(100, 100, 0))
bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
btr = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
pll = line1.joinentity(line2)
btr.AppendEntity(pll)
tr.AddNewlyCreatedDBObject(pll, True)
tr.Commit()
End Using
End Sub
End Class
End Namespace
我的autocad自定义部分要求我将两行连接到一个实体中。我一直在努力完成它。但是,我面临一些障碍。
第一个: pll = line1.joinentity(line2) 上面的行给了我一个警告'表达式不会产生值'。 我理解的是joinentity函数返回void,所以我不能将它分配给折线类型的pll。但是,我需要编写通过将两行连接到autocad数据库而形成的折线。我该如何实现这一目标?
第二个: 每当我构建和调试代码以使用Autocad进行测试时,同一行 pll = line1.joinentity(line2) 将生成错误并破坏代码。我试图理解错误信息,但无济于事。它读取
AcdbMgd.dll中出现“Autodesk.AutoCAD.Runtime.Exception”类型的异常,但未在用户代码中处理 其他信息:eNotApplicable 如果存在此异常的处理程序,则可以安全地继续该程序。
如何解决这个问题?有人可以解释一下并帮助我吗?
已编辑的代码(但问题仍然存在):
Imports System
Imports System.Runtime.InteropServices
Imports System.Drawing
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.ApplicationServices.DocumentExtension
Namespace sweeping
Public Class Testing
<CommandMethod("jointwolines")>
Public Shared Sub jointwolines()
Dim line1as polyline
Dim line2 As Line
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim bt As BlockTable
Dim btr As BlockTableRecord
Using tr As Transaction = db.TransactionManager.StartTransaction()
line1 = New Polyline()
line1.AddVertexAt(0, New Point2d(0, 0), 0, 0, 0)
line1.AddVertexAt(0, New Point2d(100, 0), 0, 0, 0)
line1.Elevation = 0
line2 = New Line(new point3d(0,0,0), new point3d(0,0,100))
bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
btr = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
btr.AppendEntity(line1)
line1.JoinEntity(line2)
tr.AddNewlyCreatedDBObject(line1, True)
tr.Commit()
End Using
End Sub
End Class
End Namespace
有人能帮助我吗?这令人沮丧:(
答案 0 :(得分:1)
JoinEntity
不会创建新实体,它会修改调用它的实体。这就是你无法获得回报价值的原因。
如果有共线,可以连接两条线,这不是你的情况。您需要在折线上调用此方法,就像在第二个代码段中一样。
来自Autodesk doc:
Polyline.JoinEntity(ies)
要求给定的实体为其他实体, 未封闭的折线或Polyline2d,线和/或弧实体,其中 分享共同的起点或终点。
但是Polyline
是一个2D实体,默认情况下位于WCS的XY平面中,并且您尝试添加一条终点,该终点在此XY平面上方100个单位。您需要使用Polyline3d
:
<CommandMethod("JOINTWOLINES")>
Public Shared Sub JoinTwoLines()
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Using tr As Transaction = db.TransactionManager.StartTransaction()
Dim bt As BlockTable
bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim btr As BlockTableRecord
btr = tr.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
Dim pl as Polyline3d
pl = New Polyline3d(Poly3dType.SimplePoly, _
new Point3dCollection(), _
False)
btr.AppendEntity(pl)
tr.AddNewlyCreatedDBObject(pl, True)
Dim line1 As Line
line1 = New Line(New Point3d(0, 0, 0), New Point3d(100, 0, 0))
btr.AppendEntity(line1)
tr.AddNewlyCreatedDBObject(line1, True)
Dim line2 As Line
line2 = New Line(new Point3d(0, 0, 0), new Point3d(0, 0, 100))
btr.AppendEntity(line2)
tr.AddNewlyCreatedDBObject(line2, True)
pl.JoinEntities(new Entity(){line1, line2})
tr.Commit()
End Using
End Sub
还有一件事:不要将变量放在sub的顶部。你不是写Pascal而是编写C#。 Declare your variables where there are used.