zzyong00 发表于 2014-11-22 23:09:11

增加一个判断点在pl曲线内侧还是外侧

'在模块中添加以下代码
Public Enum InOut
    Inside = -1
    Outside = 1
End Enum

Private Function InOutside(pl As AcadLWPolyline, P1 As Variant) As long
    'PL是要标注的PL线,P1是要监测是否在曲线内的点,三维DOUBLE数组
    '判断标注位置是否在PL范围内,可以设定坐标标在范围内还是外面
    'intInOut=-1是内侧,intInOut=1是外侧,intInOut=0是不确定是内还是外
    Dim Ppl   As Variant
    Dim tmpPL As AcadLWPolyline
    Dim i   As Integer
    Set tmpPL = pl.Copy

    tmpPL.Closed = True
    tmpPL.Elevation = 0
    Ppl = tmpPL.Coordinates
    Dim dblYmax As Double                                                       'Y坐标最大值
    dblYmax = Ppl(1)
    For i = 3 To UBound(Ppl) Step 2                                          
      If dblYmax < Ppl(i) Then dblYmax = Ppl(i)
    Next i
    Dim tmpP(2) As Double                                                       '临时点
    tmpP(0) = P1(0)
    tmpP(1) = dblYmax + 100
    tmpP(2) = 0
    Dim objL As AcadLine
    Set objL = ThisDrawing.ModelSpace.AddLine(P1, tmpP)

    'ZoomAll
    Dim dblPoints As Variant
    dblPoints = objL.IntersectWith(tmpPL, acExtendNone)
    tmpPL.Delete
    objL.Delete                                                               '清理战场
    'Debug.Print VarType(dblPoints) '即使没有交点,也是一个空的三维数组
    If UBound(dblPoints) = -1 Then                                          
      InOutside = Outside                                                   
      Exit Function
    End If
    If ((UBound(dblPoints) - LBound(dblPoints) + 1) / 3) Mod 2 Then             '交点个数为奇数,就在内侧;为偶数,就在外侧
      InOutside = Inside
    Else
      InOutside = Outside
    End If
    'Debug.Print InOutside
End Function



254619324 发表于 2014-12-13 11:03:57

楼主非常厉害

lafare 发表于 2015-1-1 22:16:39

vba也好景不长了,在64位vba7中不能调用vb6写的ActiveX DLL了,很郁闷~~~

zctao1966 发表于 2015-1-20 13:00:31

”引用“ Autocad 200* Type Library这个引用,如果没装其它版本,怎么同时引用呢,才可以在其它版本都能用

zctao1966 发表于 2015-1-23 08:55:38

感谢你的回复,如何后期绑定,请给个例子

zzyong00 发表于 2015-1-25 14:34:31

本帖最后由 zzyong00 于 2015-1-25 14:35 编辑

今天再发一下求点集凸包和最小外接矩形的代码!

首先是测试代码,在vb窗体上放一个按钮,添加如下代码(其它未定义函数详见本贴前面的代码):
Private Sub cmd凸包_Click()
    Dim objSset As AcadSelectionSet
    Dim objDoc As AcadDocument
    Set objDoc = ThisDrawing()
    AppActivate objCad.Caption
    SelectLots "MEA~PL~TMP~123", "point"
    Set objSset = objDoc.SelectionSets("MEA~PL~TMP~123")
    If objSset.Count = 0 Then Exit Sub
    objDoc.SetVariable "MODEMACRO", "计算中,请不在Autocad中执行其它操作!"
    Dim obj1 As AcadPoint ' AcadCircle ' AcadLWPolyline
    Dim i As Long
    ReDim pt(objSset.Count - 1)
    ReDim stack(objSset.Count - 1)
    For Each obj1 In objSset
      pt(i).x = obj1.Coordinates(0)
      pt(i).y = obj1.Coordinates(1)
      i = i + 1
    Next
    Call Hull(objSset.Count - 1)
    Dim Coords() As Double
    ReDim Coords(2 * (lngTop + 1) - 1)
    For i = 0 To lngTop
      Coords(2 * i) = stack(i).x
      Coords(2 * i + 1) = stack(i).y
    Next i
    Dim objPL As AcadLWPolyline
    Set objPL = objDoc.ModelSpace.AddLightWeightPolyline(Coords)
    objPL.Closed = True
    objDoc.Regen acActiveViewport
   
    Dim Coords2() As Double
   
    calMinRect lngTop, Coords2
    Set objPL = objDoc.ModelSpace.AddLightWeightPolyline(Coords2)
    objPL.Closed = True
    objDoc.Regen acActiveViewport
    objDoc.SetVariable "MODEMACRO", ""
   
End Sub
接下来是最主要的计算模块,编写的比较累,略收几个币,安慰一下自己:


charlesli8 发表于 2015-2-1 22:11:06

楼主你下了不少功夫写这个插件啊!受教了!代码不错!

_梦游- 发表于 2015-2-2 13:09:27

这个必须顶!

charlesli8 发表于 2015-2-2 20:59:04

'//极角比较, 返回-1: p0p1 在 p0p2 的右侧,返回 0:p0,p1,p2 共线
Public Function Cmp_PolarAngel(P1 As point, P2 As point, pb As point) As Long
    Dim delta As Double
    delta = (P1.x - pb.x) * (P2.y - pb.y) - (P2.x - pb.x) * (P1.y - pb.y)
    If delta < 0# Then
      Cmp_PolarAngel = 1
    ElseIf delta = 0# Then
      Cmp_PolarAngel = 0
    Else
      Cmp_PolarAngel = -1
    End If这是个测试函数,里面的x y是怎么来的?还有delta = (P1.x - pb.x) * (P2.y - pb.y) - (P2.x - pb.x) * (P1.y - pb.y) 这句是怎么算的呀?

charlesli8 发表于 2015-2-3 12:40:28

在那里问了后,过会就不见了。更没有看到你的回复。   
页: 1 2 3 [4] 5 6 7 8 9 10 11 12 13
查看完整版本: 用VB6进行Autocad的二次开发(原创)