Reputation: 11
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
Part of my autocad customization requires me to join two lines into an entity. I have been trying really hard to get it done. But, I m facing a few obstacles.
The first one: pll = line1.joinentity(line2) the line above gives me a warning 'expression does not produce a value'. What I understand is that the joinentity function returns void, so I cant assign it to the pll of polyline type. However, I need to write the polyline formed by joining two lines to the autocad database. How do I go about achieving that?
Second one: Whenever I build and debug the code to test it with Autocad, the same line pll = line1.joinentity(line2) will generate error and break the code. I tried to understand the error message, but to no avail. It reads
An exception of type 'Autodesk.AutoCAD.Runtime.Exception' occurred in AcdbMgd.dll but was not handled in user code Additional information: eNotApplicable If there is a handler for this exception, the program may be safely continued.
How to solve this problem? Can someone please explain and help me?
Edited code(but problem still persist):
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
Can someone help me? This is getting frustrating :(
Upvotes: 0
Views: 1854
Reputation: 13306
JoinEntity
does not create a new entity, it modifies the entity on which it is called. It's why you cannot get a return value.
Two lines can be joined if there are collinear, which is not your case. You need to call this method on a polyline, like you do in your second code snippet.
From the Autodesk doc :
Polyline.JoinEntity(ies)
requires the given entities to be other, unclosed Polyline or Polyline2d, Line, and/or Arc entities, which share common start or end points.
But Polyline
is a 2D entity, lying in the XY plane of the WCS by default, and you try to add a line with an end point which is 100 units above this XY plane. You need to use a 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
One more thing: do not put your variable at the top of your sub. You're not writing Pascal but C#. Declare your variables where there are used.
Upvotes: 1