明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2327|回复: 1

一个修测曲线(Vba开发)的例子

[复制链接]
发表于 2006-2-2 16:40:00 | 显示全部楼层 |阅读模式

Sub xcqx()
On Error GoTo xu
Dim mysel As AcadSelectionSet
Dim xzd As Variant
Dim addp As Variant
Dim xzd1(0 To 2) As Double
Dim bzuo As Variant
Dim lin As AcadLWPolyline
Dim lim As Acad3DPolyline
If ThisDrawing.SelectionSets.Count = 0 Then
Set mysel = ThisDrawing.SelectionSets.Add("mysel")
AppActivate ThisDrawing.Application.Caption
ThisDrawing.SetVariable "OSMODE", 512
'ThisDrawing.ObjectSnapMode = True
xzd = ThisDrawing.Utility.GetPoint(, "修测起点:")
mysel.SelectAtPoint xzd
Else
ThisDrawing.SelectionSets.Item(0).Delete
Set mysel = ThisDrawing.SelectionSets.Add("mysel")
AppActivate ThisDrawing.Application.Caption
ThisDrawing.SetVariable "OSMODE", 512
'ThisDrawing.ObjectSnapMode = True
xzd = ThisDrawing.Utility.GetPoint(, "修测起点:")
mysel.SelectAtPoint xzd
End If

xzd1(0) = xzd(0): xzd1(1) = xzd(1): xzd1(2) = xzd(2)
If mysel.Count = 1 Then
mysel(0).Highlight True
ThisDrawing.ActiveLayer = ThisDrawing.Layers(mysel(0).Layer)
If mysel(0).EntityType = 24 Then '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>二维线
ReDim zuob1(0 To 1) As Double
zuob1(0) = xzd(0): zuob1(1) = xzd(1)
On Error GoTo we
ThisDrawing.SetVariable "OSMODE", 0
For i = 1 To 1000
addp = ThisDrawing.Utility.GetPoint(xzd, "请输入")
xzd(0) = addp(0): xzd(1) = addp(1): xzd(2) = addp(2)
If Not lin Is Nothing Then
lin.Delete
End If

ReDim Preserve zuob1(0 To 2 * (i + 1) - 1) As Double
zuob1(2 * (i + 1) - 2) = addp(0): zuob1(2 * (i + 1) - 1) = addp(1)
Set lin = ThisDrawing.ModelSpace.AddLightWeightPolyline(zuob1)
lin.Highlight True
Next
we:
If Not lin Is Nothing Then
bzuo = mysel(0).Coordinates
For i = 0 To UBound(bzuo) - 2 Step 2
    dis1 = Sqr((bzuo(i) - bzuo(i + 2)) ^ 2 + (bzuo(i + 1) - bzuo(i + 3)) ^ 2)
    dis2 = Sqr((bzuo(i) - xzd1(0)) ^ 2 + (bzuo(i + 1) - xzd1(1)) ^ 2) + Sqr((bzuo(i + 2) - xzd1(0)) ^ 2 + (bzuo(i + 3) - xzd1(1)) ^ 2)
    If dis2 - dis1 <= 0.1 Then
       m = (i + 2) / 2
    End If
    dis1 = Sqr((bzuo(i) - bzuo(i + 2)) ^ 2 + (bzuo(i + 1) - bzuo(i + 3)) ^ 2)
    dis2 = Sqr((bzuo(i) - xzd(0)) ^ 2 + (bzuo(i + 1) - xzd(1)) ^ 2) + Sqr((bzuo(i + 2) - xzd(0)) ^ 2 + (bzuo(i + 3) - xzd(1)) ^ 2)
    If dis2 - dis1 <= 0.1 Then
       n = (i + 2) / 2
    End If
Next

ReDim zuob(0 To UBound(zuob1) + 2 * (-Abs(m - n)) + UBound(bzuo) + 1) As Double
If m > n Then
   lk = 0
   For j = 0 To UBound(bzuo) Step 2
       If j / 2 + 1 <= n Then
          zuob(lk) = bzuo(j): zuob(lk + 1) = bzuo(j + 1)
          lk = lk + 2
       ElseIf j / 2 + 1 = n + 1 Then
          For k = UBound(zuob1) To 0 Step -2
          zuob(lk) = zuob1(k - 1): zuob(lk + 1) = zuob1(k)
          lk = lk + 2
          Next
       ElseIf j / 2 + 1 > m Then
          zuob(lk) = bzuo(j): zuob(lk + 1) = bzuo(j + 1)
          lk = lk + 2
       End If
   Next

ElseIf n > m Then
   lk = 0
   For j = 0 To UBound(bzuo) Step 2
       If j / 2 + 1 <= m Then
          zuob(lk) = bzuo(j): zuob(lk + 1) = bzuo(j + 1)
          lk = lk + 2
       ElseIf j / 2 + 1 = m + 1 Then
          For k = 0 To UBound(zuob1) Step 2
          zuob(lk) = zuob1(k): zuob(lk + 1) = zuob1(k + 1)
          lk = lk + 2
          Next
       ElseIf j / 2 + 1 > n Then
          zuob(lk) = bzuo(j): zuob(lk + 1) = bzuo(j + 1)
          lk = lk + 2
       End If
   Next

ElseIf n = m Then
ReDim zuob(0 To UBound(zuob1) + UBound(bzuo) + 1) As Double
lk = 0
For j = 0 To UBound(bzuo) Step 2
       If j / 2 + 1 < m Then
          zuob(lk) = bzuo(j): zuob(lk + 1) = bzuo(j + 1)
          lk = lk + 2
       ElseIf j / 2 + 1 = m Then
          zuob(lk) = bzuo(j): zuob(lk + 1) = bzuo(j + 1)
          lk = lk + 2
          dis01 = Sqr((bzuo(j) - zuob1(0)) ^ 2 + (bzuo(j + 1) - zuob1(1)) ^ 2)
          dis02 = Sqr((bzuo(j) - zuob1(UBound(zuob1) - 1)) ^ 2 + (bzuo(j + 1) - zuob1(UBound(zuob1))) ^ 2)
          If dis01 < dis02 Then
          For k = 0 To UBound(zuob1) Step 2
          zuob(lk) = zuob1(k): zuob(lk + 1) = zuob1(k + 1)
          lk = lk + 2
          Next
          Else
          For k = UBound(zuob1) To 0 Step -2
          zuob(lk) = zuob1(k - 1): zuob(lk + 1) = zuob1(k)
          lk = lk + 2
          Next
          End If
       ElseIf j / 2 + 1 > m Then
          zuob(lk) = bzuo(j): zuob(lk + 1) = bzuo(j + 1)
          lk = lk + 2
       End If
Next
End If
If mysel(0).Closed = True Then
Set lin1 = ThisDrawing.ModelSpace.AddLightWeightPolyline(zuob)
lin1.Elevation = mysel(0).Elevation
lin1.Closed = True
lin1.Update
Else
Set lin1 = ThisDrawing.ModelSpace.AddLightWeightPolyline(zuob)
lin1.Elevation = mysel(0).Elevation
lin1.Update
End If
mysel(0).Delete
lin.Delete
End If

ElseIf mysel(0).EntityType = 2 Then '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>三维线
ReDim zuob1(0 To 2) As Double
zuob1(0) = xzd(0): zuob1(1) = xzd(1): zuob1(2) = xzd(2)
On Error GoTo we1
ThisDrawing.SetVariable "OSMODE", 0
For i = 1 To 1000
addp = ThisDrawing.Utility.GetPoint(xzd, "请输入")
xzd(0) = addp(0): xzd(1) = addp(1): xzd(2) = xzd1(2)

If Not lim Is Nothing Then
lim.Delete
End If

ReDim Preserve zuob1(0 To 3 * (i + 1) - 1) As Double
zuob1(3 * (i + 1) - 3) = addp(0): zuob1(3 * (i + 1) - 2) = addp(1): zuob1(3 * (i + 1) - 1) = xzd1(2)
Set lim = ThisDrawing.ModelSpace.Add3DPoly(zuob1)
lim.Highlight True
Next
we1:
If Not lim Is Nothing Then
bzuo = mysel(0).Coordinates
For i = 0 To UBound(bzuo) - 3 Step 3
    dis1 = Sqr((bzuo(i) - bzuo(i + 3)) ^ 2 + (bzuo(i + 1) - bzuo(i + 4)) ^ 2)
    dis2 = Sqr((bzuo(i) - xzd1(0)) ^ 2 + (bzuo(i + 1) - xzd1(1)) ^ 2) + Sqr((bzuo(i + 3) - xzd1(0)) ^ 2 + (bzuo(i + 4) - xzd1(1)) ^ 2)
    If dis2 - dis1 <= 0.1 Then
       m = (i + 3) / 3
    End If
    dis1 = Sqr((bzuo(i) - bzuo(i + 3)) ^ 2 + (bzuo(i + 1) - bzuo(i + 4)) ^ 2)
    dis2 = Sqr((bzuo(i) - xzd(0)) ^ 2 + (bzuo(i + 1) - xzd(1)) ^ 2) + Sqr((bzuo(i + 3) - xzd(0)) ^ 2 + (bzuo(i + 4) - xzd(1)) ^ 2)
    If dis2 - dis1 <= 0.1 Then
       n = (i + 3) / 3
    End If
Next

ReDim zuob(0 To UBound(zuob1) + 3 * (-Abs(m - n)) + UBound(bzuo) + 1) As Double
If m > n Then
   lk = 0
   For j = 0 To UBound(bzuo) Step 3
       If j / 3 + 1 <= n Then
          zuob(lk) = bzuo(j): zuob(lk + 1) = bzuo(j + 1): zuob(lk + 2) = bzuo(j + 2)
          lk = lk + 3
       ElseIf j / 3 + 1 = n + 1 Then
          For k = UBound(zuob1) To 0 Step -3
          zuob(lk) = zuob1(k - 2): zuob(lk + 1) = zuob1(k - 1): zuob(lk + 2) = zuob1(k)
          lk = lk + 3
          Next
       ElseIf j / 3 + 1 > m Then
          zuob(lk) = bzuo(j): zuob(lk + 1) = bzuo(j + 1): zuob(lk + 2) = bzuo(j + 2)
          lk = lk + 3
       End If
   Next

ElseIf n > m Then
   lk = 0
   For j = 0 To UBound(bzuo) Step 3
       If j / 3 + 1 <= m Then
          zuob(lk) = bzuo(j): zuob(lk + 1) = bzuo(j + 1): zuob(lk + 2) = bzuo(j + 2)
          lk = lk + 3
       ElseIf j / 3 + 1 = m + 1 Then
          For k = 0 To UBound(zuob1) Step 3
          zuob(lk) = zuob1(k): zuob(lk + 1) = zuob1(k + 1): zuob(lk + 2) = zuob1(k + 2)
          lk = lk + 3
          Next
       ElseIf j / 3 + 1 > n Then
          zuob(lk) = bzuo(j): zuob(lk + 1) = bzuo(j + 1): zuob(lk + 2) = bzuo(j + 2)
          lk = lk + 3
       End If
   Next

ElseIf n = m Then
ReDim zuob(0 To UBound(zuob1) + UBound(bzuo) + 1) As Double
lk = 0
For j = 0 To UBound(bzuo) Step 3
       If j / 3 + 1 < m Then
          zuob(lk) = bzuo(j): zuob(lk + 1) = bzuo(j + 1): zuob(lk + 2) = bzuo(j + 2)
          lk = lk + 3
       ElseIf j / 3 + 1 = m Then
          zuob(lk) = bzuo(j): zuob(lk + 1) = bzuo(j + 1): zuob(lk + 2) = bzuo(j + 2)
          lk = lk + 3
          dis01 = Sqr((bzuo(j) - zuob1(0)) ^ 2 + (bzuo(j + 1) - zuob1(1)) ^ 2)
          dis02 = Sqr((bzuo(j) - zuob1(UBound(zuob1) - 2)) ^ 2 + (bzuo(j + 1) - zuob1(UBound(zuob1) - 1)) ^ 2)
          If dis01 < dis02 Then
          For k = 0 To UBound(zuob1) Step 3
          zuob(lk) = zuob1(k): zuob(lk + 1) = zuob1(k + 1): zuob(lk + 2) = zuob1(k + 2)
          lk = lk + 3
          Next
          Else
          For k = UBound(zuob1) To 0 Step -3
          zuob(lk) = zuob1(k - 2): zuob(lk + 1) = zuob1(k - 1): zuob(lk + 2) = zuob1(k)
          lk = lk + 3
          Next
          End If
       ElseIf j / 3 + 1 > m Then
          zuob(lk) = bzuo(j): zuob(lk + 1) = bzuo(j + 1): zuob(lk + 2) = bzuo(j + 2)
          lk = lk + 3
       End If
Next
End If
If mysel(0).Closed = True Then
Set lim1 = ThisDrawing.ModelSpace.Add3DPoly(zuob)
lim1.Closed = True
lim1.Update
Else
Set lim1 = ThisDrawing.ModelSpace.Add3DPoly(zuob)
lim1.Update
End If
mysel(0).Delete
lim.Delete
End If
End If
If mysel.Count <> 0 Then
mysel.Delete
End If
End If
xu:
End Sub

    与大家交流,提高自己的水平!!!!

发表于 2012-2-6 09:23:50 | 显示全部楼层
如果目标的是两条线,如何修测?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-12-23 18:05 , Processed in 0.181640 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表