田草博客

互联网田草博客


网友交流QQ群:11740834 需注明申请加入原因

微信 公众号:ByCAD

邮箱:tiancao1001x126.com
ByCAD,微信公众号
首页 | 普通 | 电脑 | AutoCAD | VB/VB.NET | FLash | 结构 | 建筑 | 电影 | BIM | 规范 | 软件 | ID
-随机-|-分布-
-博客论坛-|-﨣﨤﨧﨨-
-网站导航-|-规范下载-
-BelovedFLash欣赏-

用户登陆
用户:
密码:
 

站点日历
73 2024 - 11 48
     12
3456789
10111213141516
17181920212223
24252627282930


站点统计

最新评论



点到直线的垂足 <阿甘正传>生命就像一盒巧克力
未知 把直线、圆弧合并成多段线(PL线)   [ 日期:2011-12-26 ]   [ 来自:代码转换 ]  HTML
程序代码:


(defun c:tt ()   
    (setvar "cmdecho" 0)
    (setvar "peditaccept" 1)
    (if (setq ss (ssget '((0 . "LINE,ARC,LWPOLYLINE,POLYLINE"))))
        (command "_pedit" "m" ss "" "j" "0.1" "")
    )
  (princ)
)

多线连接
(defun c:bdx( / ss i ename endata entype)
(princ "\n选择线段 <右键选择集>: ")
(setq ss (ssget '((0 . "LINE,LWPOLYLINE,ARC"))))
      (command "PEDITACCEPT" "1")
(if ss
      (command ".pedit" "m" ss "" "j" "0" "w" "0" "" "PEDITACCEPT" "0")
      (command ".pedit" "m" "p" "" "j" "0" "w" "0" "" "PEDITACCEPT" "0")
))

原文地址:http://www.objectarx.net/foru ... wthread&tid=1466
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.ApplicationServices
Public Class 把直线和圆弧合并成PL '代码来自网络http://www.objectarx.net/foru ... wthread&tid=1466
    '把直线和圆弧合并成PL线
    <CommandMethod("ToPLTest")> _
    Sub MyPolyline()
        Dim Ed As Autodesk.AutoCAD.EditorInput.Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
        Dim DocLock As DocumentLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument()
        Dim Values(0) As TypedValue
        Values.SetValue(New TypedValue(DxfCode.Start, "Line,Arc"), 0)
        Dim sFilter As SelectionFilter = New SelectionFilter(Values)
        Dim Opts As PromptSelectionOptions = New PromptSelectionOptions()
        Opts.MessageForAdding = "选择线与圆弧"
        Dim Res As PromptSelectionResult
        Res = Ed.GetSelection(Opts, sFilter)
        If Res.Status = PromptStatus.OK Then
            If Not Res.Value Is Nothing Then
                Dim SS As Autodesk.AutoCAD.EditorInput.SelectionSet = Res.Value
                Dim IDArray As ObjectId() = SS.GetObjectIds()
                Dim Obc As ObjectIdCollection = New ObjectIdCollection()
                For Each Id As ObjectId In IDArray
                    Obc.Add(Id)
                Next
                If Obc.Count > 1 Then
                    DwgPolyLine(Obc)
                    For Each Id As ObjectId In IDArray
                        Dim Db As Database = HostApplicationServices.WorkingDatabase
                        Using trans As Transaction = Db.TransactionManager.StartTransaction()
                            Dim Ent As Entity = trans.GetObject(Id, OpenMode.ForWrite, False)
                            Ent.Erase()
                            trans.Commit()
                        End Using
                    Next
                End If
            End If
        End If
        DocLock.Dispose()
    End Sub
    Private Sub DwgPolyLine(ByVal ObjectIdC As ObjectIdCollection) '画多段线 
        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
        Dim Pl As New Polyline
        Dim tObjectId As ObjectIdCollection = ObjectIdC
        Dim WhileBo As Boolean = True
        Dim LName As String = ""
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Using trans As Transaction = db.TransactionManager.StartTransaction()
            Dim E As Entity = trans.GetObject(tObjectId(0), OpenMode.ForRead)
            LName = E.Layer '获取第一个对象的图层,用来保持合并成的PL线,在原来的的图层上
        End Using
        Do While WhileBo
            For Each Id As ObjectId In tObjectId
                Using trans As Transaction = db.TransactionManager.StartTransaction()
                    Dim C As Curve = trans.GetObject(Id, OpenMode.ForWrite, False)
                    Dim EntName As String = C.GetType.Name
                    If Pl.NumberOfVertices = 0 Then
                        If EntName = "Arc" Then '如果是圆弧,设置polyline的凸起值 
                            Pl.AddVertexAt(0, C.StartPoint.Convert2d(New Plane), GetBulge(C, True), 0, 0)
                        Else
                            Pl.AddVertexAt(0, C.StartPoint.Convert2d(New Plane), 0, 0, 0)
                        End If
                        Pl.AddVertexAt(1, C.EndPoint.Convert2d(New Plane), 0, 0, 0)
                        tObjectId.Remove(Id) '该实体已经合并完,删除该实体ID在组数 
                        WhileBo = True
                        Exit For
                    Else
                        If C.StartPoint = Pl.GetPoint3dAt(0) Then
                            If EntName = "Arc" Then
                                Pl.AddVertexAt(0, C.EndPoint.Convert2d(New Plane), GetBulge(C, False), 0, 0)
                            Else
                                Pl.AddVertexAt(0, C.EndPoint.Convert2d(New Plane), 0, 0, 0)
                            End If
                            tObjectId.Remove(Id)
                            WhileBo = True
                            Exit For
                        ElseIf C.EndPoint = Pl.GetPoint3dAt(0) Then
                            If EntName = "Arc" Then
                                Pl.AddVertexAt(0, C.StartPoint.Convert2d(New Plane), GetBulge(C, True), 0, 0)
                            Else
                                Pl.AddVertexAt(0, C.StartPoint.Convert2d(New Plane), 0, 0, 0)
                            End If
                            tObjectId.Remove(Id)
                            WhileBo = True
                            Exit For
                        ElseIf C.StartPoint = Pl.GetPoint3dAt(Pl.NumberOfVertices - 1) Then
                            If EntName = "Arc" Then
                                Pl.SetBulgeAt(Pl.NumberOfVertices - 1, GetBulge(C, True))
                            End If
                            Pl.AddVertexAt(Pl.NumberOfVertices, C.EndPoint.Convert2d(New Plane), 0, 0, 0)
                            tObjectId.Remove(Id)
                            WhileBo = True
                            Exit For
                        ElseIf C.EndPoint = Pl.GetPoint3dAt(Pl.NumberOfVertices - 1) Then
                            If EntName = "Arc" Then
                                Pl.SetBulgeAt(Pl.NumberOfVertices - 1, GetBulge(C, False))
                            End If
                            Pl.AddVertexAt(Pl.NumberOfVertices, C.StartPoint.Convert2d(New Plane), 0, 0, 0)
                            tObjectId.Remove(Id)
                            WhileBo = True
                            Exit For
                        Else
                            WhileBo = False
                        End If
                    End If
                    trans.Commit()
                End Using
            Next
            If tObjectId.Count < 1 Then
                WhileBo = False
            End If
        Loop
        Pl.Layer = LName
        DrawEntity(Pl) '把新建的PolyLine加到数据库
        If tObjectId.Count > 0 Then '判断是否把线与圆弧合并完 
            DwgPolyLine(tObjectId)
        End If
    End Sub
    '计算凸起值
    Private Function GetBulge(ByVal A As Arc, ByVal SEbo As Boolean) As Double
        Dim Bulge As Double
        Dim L1 As Double = A.StartPoint.GetVectorTo(A.EndPoint).Length
        Dim Angle As Double = A.EndAngle - A.StartAngle
        If Angle < 0 Then
            Angle = Math.PI * 2 + Angle '计算圆弧总角度 
        End If
        If Angle > Math.PI Then '判断是否大于180度 
            Bulge = A.Radius + Math.Sqrt(A.Radius ^ 2 - (L1 / 2) ^ 2) '计算凸起值 
        Else
            Bulge = A.Radius - Math.Sqrt(A.Radius ^ 2 - (L1 / 2) ^ 2) '计算凸起值 
        End If
        Dim Pt2 As Point3d = A.GetPointAtDist(A.GetDistanceAtParameter(A.EndParam) / 2) '取中点 
        Dim TempDouble As Double
        If SEbo = True Then '判断方向 
            TempDouble = PtSide(A.StartPoint, Pt2, A.EndPoint)
        Else
            TempDouble = PtSide(A.EndPoint, Pt2, A.StartPoint)
        End If
        If TempDouble > 0 Then '判断圆弧是凸向哪边 
            Return -Bulge / (L1 / 2)
        ElseIf TempDouble < 0 Then
            Return Bulge / (L1 / 2)
        End If
    End Function
    '判断圆弧是凸向哪边 
    Private Shared Function PtSide(ByVal pt1 As Point3d, ByVal pt2 As Point3d, ByVal pt3 As Point3d) As Double
        Dim vect1 As Vector3d = pt1.GetVectorTo(pt2)
        Dim vect2 As Vector3d = pt1.GetVectorTo(pt3)
        Return vect2.X * vect1.Y - vect1.X * vect2.Y
    End Function
    Public Function DrawEntity(ByVal ent As Entity) As ObjectId '加入实体到数据库
        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
        Try
            Dim db As Database = HostApplicationServices.WorkingDatabase
            Using trans As Transaction = db.TransactionManager.StartTransaction()
                Dim bt As BlockTable = DirectCast(trans.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)
                Dim btr As BlockTableRecord = DirectCast(trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
                btr.AppendEntity(ent)
                trans.AddNewlyCreatedDBObject(ent, True)
                trans.Commit()
            End Using
            Return ent.ObjectId
        Catch ex As Exception
            ed.WriteMessage(ex.Message)
            Return ObjectId.Null
        End Try
    End Function
End Class

【把直线和圆弧合并成PL.rar】点击下载此文件


[本日志由 tiancao1001 于 2019-02-08 10:23 PM 编辑]


引用这个评论 420995017 于 2013-01-22 07:30 PM 发表评论: 
博主 麻烦发我邮箱吧  非常感谢
420995017@qq.com

引用这个评论 420995017 于 2013-01-22 07:29 PM 发表评论: 
这个不知道好不好用 找了好久的

发表评论 - 不要忘了输入验证码哦!
作者: 用户:  密码:   注册? 验证:  防止恶意留言请输入问题答案:1*9=?  
评论:

禁止表情
禁止UBB
禁止图片
识别链接
识别关键字

字体样式 文字大小 文字颜色
插入粗体文本 插入斜体文本 插入下划线
左对齐 居中对齐 右对齐
插入超级链接 插入邮件地址 插入图像
插入 Flash 插入代码 插入引用
插入列表 插入音频文件 插入视频文件
插入缩进符合
点击下载按钮 下标 上标
水平线 简介分割标记
表  情
 
Tiancao Blog All Rights Reserved 田草博客 版权所有
Copyright ©