DaveW 发表于 2006-6-21 22:16:25

将闭合 LWPolyline 转换为 2D 折线

只是以为我会分享这个。我做了一些更改,但不记得原始代码来自哪里。

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

**** Hidden Message *****

FengK 发表于 2006-6-22 02:21:54

我很好奇在什么样的情况下,人们会更喜欢2dPolyline而不是LwPolyline? 我总是以相反的方式执行转换。谢谢。

DaveW 发表于 2006-6-22 02:39:40

Kelie,
我的软件的一部分在3D实体上做功能识别。一旦我有了从3D实体中分解的线条和弧线,我就运行Pedit将事物连接成折线。将 PLintype 系统变量设置为 2 时,它在复杂路径上工作得更好。这将生成一个 LWPolyline。然后,我的软件输出到13个不同的CAM软件。其中一些只能识别2DPolyline。
格伦,
只是避免命令行的另一种时间方式我希望我有一个好的例程,将片段连接成折线,而不是使用pedit,但似乎没有其他东西比我尝试过的更好。

Bryco 发表于 2006-6-23 09:57:14

试试这里http://www.theswamp.org/index.php?topic=10290.msg131276#msg13127它是用cnc编写的

DaveW 发表于 2006-6-23 10:29:31


非常感谢。我知道我在这里看到了一些代码,我想尝试一下,但忘记了它在哪里。目前没有时间,但几天后我会认真检查。我确信这是任何地方最好的代码。
You RAWK!
再次感谢并保重,
Dave
页: [1]
查看完整版本: 将闭合 LWPolyline 转换为 2D 折线