Author Topic: Convert Closed LWPolyline to 2D Polyline  (Read 4800 times)

0 Members and 1 Guest are viewing this topic.

DaveW

  • Guest
Convert Closed LWPolyline to 2D Polyline
« on: June 21, 2006, 10:16:25 PM »
Just thought I would share this. I amde some changes and do not remember where the original code came from.

Code: [Select]

Private Function polyentconvert2(polyEnt As Object) As AcadPolyline
Dim entity As AcadDocument
Set entity = AutoCAD_Application.ActiveDocument

Dim I As Integer, j As Integer, K As Integer
Dim EN2 As AcadPolyline
Dim b As Double, w As Double, W2 As Double
Dim PolyZPosition As Double
Dim polyentx As AcadPolyline
Dim FromNewPoint(0 To 2) As Double
Dim MoveNewPoint(0 To 2) As Double
 
 
 If polyEnt.EntityName = "AcDbPolyline" Then
  Dim Coords As Variant
  Coords = polyEnt.Coordinates
  I = Fix((UBound(Coords) + 1) * 1.5) - 1
 
  If I = 5 Then
 
  GoTo newconvert
 
  End If
  ReDim Coords2(I) As Double
  j = 0
  Dim X As Double, y As Double, z As Double
 
  For I = LBound(Coords) To UBound(Coords) Step 2
    X = Coords(I): y = Coords(I + 1): z = 0#
    Coords2(j) = X:
    Coords2(j + 1) = y:
    Coords2(j + 2) = z:
    j = j + 3
  Next I
  Dim Coords2V As Variant
  Coords2V = Coords2

  Set EN2 = entity.ModelSpace.AddPolyline(Coords2V)
  EN2.Closed = polyEnt.Closed
  EN2.Color = polyEnt.Color
  EN2.Linetype = polyEnt.Linetype
  EN2.Thickness = polyEnt.Thickness
    EN2.Layer = polyEnt.Layer
   
 
  For I = 0 To UBound(Coords) Step 2
    j = I / 2
    b = polyEnt.GetBulge(j)
    polyEnt.GetWidth j, w, W2
    EN2.SetBulge j, b
    EN2.SetWidth j, w, W2
  Next I
 

 
  Set polyentx = EN2
 

  polyEnt.GetBoundingBox minExt, maxExt
  PolyZPosition = Round(maxExt(2), 5)
               
               
  polyEnt.Delete
 

    FromNewPoint(0) = 0
    FromNewPoint(1) = 0
    FromNewPoint(2) = 0
    MoveNewPoint(0) = 0
    MoveNewPoint(1) = 0
    MoveNewPoint(2) = PolyZPosition
    EN2.Move FromNewPoint, MoveNewPoint

 End If
 GoTo endhere
 
 
 
newconvert:
 
 
I = I + 3

ReDim Coords2(I) As Double
 

Dim newcords As Variant


On Error Resume Next
Coords2(0) = Coords(0)
Coords2(1) = Coords(1)
Coords2(2) = 0

Coords2(3) = Coords(2)
Coords2(4) = Coords(3)
Coords2(5) = 0

Coords2(6) = Coords(0)
Coords2(7) = Coords(1)
Coords2(8) = 0


newcords = Coords2
 
 
    Set EN2 = entity.ModelSpace.AddPolyline(newcords)
    EN2.Closed = polyEnt.Closed
    EN2.Color = polyEnt.Color
    EN2.Linetype = polyEnt.Linetype
    EN2.Thickness = polyEnt.Thickness
    EN2.Layer = polyEnt.Layer
 
  For I = 0 To UBound(Coords) Step 2
    j = I / 2
    b = polyEnt.GetBulge(j)
    EN2.SetBulge j, b

  Next I
 
  polyEnt.GetBoundingBox minExt, maxExt
  PolyZPosition = Round(maxExt(2), 5)
               
               
  polyEnt.Delete
 

    FromNewPoint(0) = 0
    FromNewPoint(1) = 0
    FromNewPoint(2) = 0
    MoveNewPoint(0) = 0
    MoveNewPoint(1) = 0
    MoveNewPoint(2) = PolyZPosition
    EN2.Move FromNewPoint, MoveNewPoint
 
 
 
 
 
 
endhere:
End Function

FengK

  • Guest
Re: Convert Closed LWPolyline to 2D Polyline
« Reply #1 on: June 22, 2006, 02:21:54 AM »
I'm curious under what kind of circumstances one would prefer 2dPolyline to LwPolyline?  I always perform conversion the other way around. Thanks.

Glenn R

  • Guest
Re: Convert Closed LWPolyline to 2D Polyline
« Reply #2 on: June 22, 2006, 02:39:40 AM »
Just type in ConvertPoly - it's a standard AutoCAD command and you can convert both ways.

DaveW

  • Guest
Re: Convert Closed LWPolyline to 2D Polyline
« Reply #3 on: June 23, 2006, 09:57:14 AM »
Kelie,

Part of my software does feature recognition on 3D solids. Once I have lines and arcs, exploded from the 3D solids, I run Pedit to join things up in polylines. With the PLintype system variable set to 2, it works much better on complicated paths. This produces a LWPolyline. My software then outputs to 13 different CAM softwares. Some of this will only recognize a 2DPolyline.


Glenn,

Just another time way to avoid the command line. I wish I had a good routine for joining segments into polylines, instead of using pedit, but nothing else seems to work as good that I have tried.


Bryco

  • Water Moccasin
  • Posts: 1883
Re: Convert Closed LWPolyline to 2D Polyline
« Reply #4 on: June 23, 2006, 10:29:31 AM »

DaveW

  • Guest
Re: Convert Closed LWPolyline to 2D Polyline
« Reply #5 on: June 23, 2006, 02:09:40 PM »
try here http://www.theswamp.org/index.php?topic=10290.msg131276#msg13127 It was written with cnc in mind


Thank you very much. I knew I saw some code here i wanted to try for that, but forgot where it was at. No time at the moment, but I will give a serious go through in a few days. I am sure it is the best code anywhere already.

You RAWK!

Thanks again and take care,

Dave