- UID
- 288
- 积分
- 0
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-1-11
- 最后登录
- 1970-1-1
|
发表于 2006-9-28 15:38:47
|
显示全部楼层
刚刚编了一段代码,做桩位图效果还形。带编号,同时按照坐标排序。
'唐僧肉写于2006.9.19
'得到园族的圆心坐标,并输入excel
'**************************
Sub cen()
'创建选择集
On Error GoTo Err_Control
Dim PI As Double: PI = Atn(1) * 4
Dim sSetObj, sSetObj2 As AcadSelectionSet
Dim gpCode(0) As Integer
Dim dataValue(0) As Variant
Dim groupCode As Variant, dataCode As Variant
Dim PNT As Variant
Dim Pnt1(0 To 2) As Double: Pnt1(0) = 0: Pnt1(1) = 0: Pnt1(2) = 0
Dim centP As Variant
Dim I As Integer: I = 0
Dim A As Integer: A = 0
Dim txtObj As AcadText
Dim n As Integer
Dim newUCS As AcadUCS
Dim CurrUCS As AcadUCS
Dim PILe_NUM, Pnum As String
Pnum = InputBox("输入墩号", 墩号, "1")
' Set sSetObj = ThisDrawing.SelectionSets.Add("SSET")
For A = 1 To 2
If A = 1 Then
PILe_NUM = "Z" & Pnum & "左"
Else
PILe_NUM = "Z" & Pnum & "右"
End If
Set sSetObj = ThisDrawing.SelectionSets.Add("SSET3")
gpCode(0) = 0
dataValue(0) = "circle"
groupCode = gpCode
dataCode = dataValue
sSetObj.SelectOnScreen groupCode, dataCode
n = sSetObj.Count
If n = 0 Then Err.Raise 1, , "未选择任何物体!"
ReDim centP(n * 3 - 1)
If ThisDrawing.GetVariable("UCSNAME") = "" Then
With ThisDrawing
Set CurrUCS = .UserCoordinateSystems.Add( _
Pnt1, _
.GetVariable("UCSXDIR"), _
.GetVariable("UCSYDIR"), _
"OriginalUCS")
CurrUCS.Origin = .GetVariable("UCSORG")
End With
For I = 0 To n - 1 Step 1
PNT = ThisDrawing.Utility.TranslateCoordinates(sSetObj(I).Center, acWorld, acUCS, 0)
centP(3 * I) = Round(PNT(0), 3)
centP(3 * I + 1) = Round(PNT(1), 3)
centP(3 * I + 2) = Round(PNT(2), 3)
'Debug.Print centP(3 * I); centP(3 * I + 1); centP(3 * I + 2); Spc(3)
Next I
Call SSortXYZ(centP, "x")
Call OnlySortxyzUP(centP, "y")
For I = 0 To n - 1 Step 1
PNT(0) = Round(centP(3 * I), 3)
PNT(1) = Round(centP(3 * I + 1), 3)
PNT(2) = Round(centP(3 * I + 2), 3)
PNT = ThisDrawing.Utility.TranslateCoordinates(PNT, acUCS, acWorld, 0)
' Debug.Print PNT(0)
' Debug.Print PNT(1)
' Debug.Print PNT(2)
centP(3 * I) = Round(PNT(0), 3)
centP(3 * I + 1) = Round(PNT(1), 3)
centP(3 * I + 2) = Round(PNT(2), 3)
Set txtObj = ThisDrawing.ModelSpace.AddText("桩位" & CStr(I + 1), PNT, 0.5)
Next I
Else
Set CurrUCS = ThisDrawing.ActiveUCS 'current UCS is saved
For I = 0 To n - 1 Step 1
PNT = ThisDrawing.Utility.TranslateCoordinates(sSetObj(I).Center, acWorld, acUCS, 0)
centP(3 * I) = Round(PNT(0), 3)
centP(3 * I + 1) = Round(PNT(1), 3)
centP(3 * I + 2) = Round(PNT(2), 3)
Debug.Print centP(3 * I); centP(3 * I + 1); centP(3 * I + 2); Spc(3)
Next I
Call SSortXYZ(centP, "x")
Call OnlySortxyzUP(centP, "y")
For I = 0 To n - 1 Step 1
PNT(0) = Round(centP(3 * I), 3)
PNT(1) = Round(centP(3 * I + 1), 3)
PNT(2) = Round(centP(3 * I + 2), 3)
PNT = ThisDrawing.Utility.TranslateCoordinates(PNT, acWorld, acUCS, 0)
Set txtObj = ThisDrawing.ModelSpace.AddText("桩位" & CStr(I + 1), PNT, 0.5)
Next I
End If
Call Cad2Xls(PILe_NUM, centP, n)
ThisDrawing.SelectionSets.Item("SSET3").Delete
Next A
'容错处理
Exit_Here:
'sSetObj.Delete
Exit Sub
Err_Control:
MsgBox Err.Description
'Resume Exit_Here
'sSetObj.Delete
End Sub
Sub Cad2Xls(ByVal PILe_NUM As String, ByVal PNT As Variant, n As Integer)
'On Error Resume Next
Dim xlApp As Excel.Application
Set xlApp = GetObject(, "Excel.Application")
If Err Then
MsgBox " Excel 没有运行。"
Exit Sub
End If
Dim xlSht As Worksheet
Set xlSht = xlApp.ActiveSheet
Dim BlockObj As AcadBlock
Set BlockObj = ThisDrawing.Blocks("*Model_Space")
Dim xlRange As Range
Dim RngCol As Integer
Dim RngRow As Double
Dim ColRow As Double
RngCol = xlSht.UsedRange.Columns.Count
If RngCol = 1 Then RngCol = 0
'RngRow = xlSheet.UsedRange.Rows.Count
' Debug.Print UBound(PNT)
' Debug.Print RngCol
'******************************************************************
For I = 0 To n - 1 Step 1
'PNT = ThisDrawing.Utility.TranslateCoordinates(PNT, acWorld, acUCS, 0)
If I = 0 Then
xlSht.Cells(I + 1, RngCol + 1).Value = PILe_NUM
xlSht.Cells(I + 1, RngCol + 2).Value = "x坐标"
xlSht.Cells(I + 1, RngCol + 3).Value = "y坐标"
End If
xlSht.Cells(I + 2, RngCol + 1).Value = "桩位" & CStr(I + 1)
xlSht.Cells(I + 2, RngCol + 2).Value = PNT(3 * I + 1) 'x坐标
xlSht.Cells(I + 2, RngCol + 3).Value = PNT(3 * I) 'Y坐标
Next I
' RngCol = xlSht.UsedRange.Columns.Count
'Debug.Print RngCol
Set xlRange = Nothing
Set xlSht = Nothing
Set xlApp = Nothing
End Sub
'对text对象的 x或 y或 z坐标排序
'调用本子例程时,用call tsortxyz(pnt,"x")
'""号内字符分别对应x,y,z坐标
'返回排完序后的3维点集及字符串
'排列顺序由大到小
Sub TSortXYZ(PNT, XYZ As String)
Dim I, J, K As Integer
Dim Val, Val1, Val2 As Double
Dim Str As String
Select Case XYZ
Case "x"
For I = LBound(PNT) To UBound(PNT) - 1 Step 4
K = I + 4
For J = K To UBound(PNT) Step 4
If PNT(I) < PNT(J) Then
Val = PNT(I): Val1 = PNT(I + 1): Val2 = PNT(I + 2): Str = PNT(I + 3)
PNT(I) = PNT(J): PNT(I + 1) = PNT(J + 1): PNT(I + 2) = PNT(J + 2): PNT(I + 3) = PNT(J + 3)
PNT(J) = Val: PNT(J + 1) = Val1: PNT(J + 2) = Val2: PNT(J + 3) = Str
End If
Next J
Next I
Case "y"
For I = LBound(PNT) + 1 To UBound(PNT) - 1 Step 4
K = I + 4
For J = K To UBound(PNT) Step 4
If PNT(I) < PNT(J) Then
Val = PNT(I - 1): Val1 = PNT(I): Val2 = PNT(I + 1): Str = PNT(I + 2)
PNT(I - 1) = PNT(J - 1): PNT(I) = PNT(J): PNT(I + 1) = PNT(J + 1): PNT(I + 2) = PNT(J + 2)
PNT(J - 1) = Val: PNT(J) = Val1: PNT(J + 1) = Val2: PNT(J + 2) = Str
'Debug.Print Str
End If
Next J
Next I
Case "z"
For I = LBound(PNT) + 2 To UBound(PNT) - 1 Step 4
K = I + 4
For J = K To UBound(PNT) Step 4
If PNT(I) < PNT(J) Then
Val = PNT(I - 2): Val1 = PNT(I - 1): Val2 = PNT(I): Str = PNT(I + 1)
PNT(I - 2) = PNT(J - 2): PNT(I - 1) = PNT(J - 1): PNT(I) = PNT(J): PNT(I + 1) = PNT(J + 1)
PNT(J - 2) = Val: PNT(J - 1) = Val1: PNT(J) = Val2: PNT(J + 1) = Str
End If
Next J
Next I
End Select
End Sub
'对 x或 y或 z坐标排序
'调用本子例程时,用call Ssortxyz(pnt,"x")
'""号内字符分别对应x,y,z坐标
'返回排完序后的3维点集
'排列顺序由小到大
Sub SSortXYZ(PNT, XYZ As String)
Dim I, J, K As Integer
Dim Val, Val1, Val2 As Double
Select Case XYZ
Case "x"
For I = LBound(PNT) To UBound(PNT) - 1 Step 3
K = I + 3
For J = K To UBound(PNT) Step 3
If PNT(I) > PNT(J) Then
Val = PNT(I): Val1 = PNT(I + 1): Val2 = PNT(I + 2)
PNT(I) = PNT(J): PNT(I + 1) = PNT(J + 1): PNT(I + 2) = PNT(J + 2)
PNT(J) = Val: PNT(J + 1) = Val1: PNT(J + 2) = Val2
End If
Next J
Next I
Case "y"
For I = LBound(PNT) + 1 To UBound(PNT) - 1 Step 3
K = I + 3
For J = K To UBound(PNT) Step 3
If PNT(I) > PNT(J) Then
Val = PNT(I - 1): Val1 = PNT(I): Val2 = PNT(I + 1)
PNT(I - 1) = PNT(J - 1): PNT(I) = PNT(J): PNT(I + 1) = PNT(J + 1)
PNT(J - 1) = Val: PNT(J) = Val1: PNT(J + 1) = Val2
End If
Next J
Next I
Case "z"
For I = LBound(PNT) + 2 To UBound(PNT) - 1 Step 3
K = I + 3
For J = K To UBound(PNT) Step 3
If PNT(I) > PNT(J) Then
Val = PNT(I - 2): Val1 = PNT(I - 1): Val2 = PNT(I)
PNT(I - 2) = PNT(J - 2): PNT(I - 1) = PNT(J - 1): PNT(I) = PNT(J)
PNT(J - 2) = Val: PNT(J - 1) = Val1: PNT(J) = Val2
End If
Next J
Next I
End Select
End Sub
'例如对 y坐标排序,x 坐标不动
'调用本子例程时,用call Ssortxyz(pnt,"x")
'""号内字符分别对应x,y,z坐标
'返回排完序后的3维点集
'排列顺序由小到大
Sub OnlySortxyzLow(PNT, XYZ As String)
Dim I, J, K As Integer
Dim Val, Val1, Val2 As Double
Select Case XYZ
Case "x"
For I = LBound(PNT) To UBound(PNT) - 1 Step 3
K = I + 3
For J = K To UBound(PNT) Step 3
If PNT(I - 1) = PNT(J - 1) Then
If PNT(I) > PNT(J) Then
Val = PNT(I): Val1 = PNT(I + 1): Val2 = PNT(I + 2)
PNT(I) = PNT(J): PNT(I + 1) = PNT(J + 1): PNT(I + 2) = PNT(J + 2)
PNT(J) = Val: PNT(J + 1) = Val1: PNT(J + 2) = Val2
End If
Else
GoTo 10
End If
10 Next J
Next I
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Case "y"
For I = LBound(PNT) + 1 To UBound(PNT) - 1 Step 3
K = I + 3
For J = K To UBound(PNT) Step 3
If PNT(I - 1) = PNT(J - 1) Then
If PNT(I) > PNT(J) Then
Val = PNT(I - 1): Val1 = PNT(I): Val2 = PNT(I + 1)
PNT(I - 1) = PNT(J - 1): PNT(I) = PNT(J): PNT(I + 1) = PNT(J + 1)
PNT(J - 1) = Val: PNT(J) = Val1: PNT(J + 1) = Val2
End If
Else
GoTo 20
End If
20 Next J
Next I
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Case "z"
For I = LBound(PNT) + 2 To UBound(PNT) - 1 Step 3
K = I + 3
For J = K To UBound(PNT) Step 3
If PNT(I - 1) = PNT(J - 1) Then
If PNT(I) > PNT(J) Then
Val = PNT(I - 2): Val1 = PNT(I - 1): Val2 = PNT(I)
PNT(I - 2) = PNT(J - 2): PNT(I - 1) = PNT(J - 1): PNT(I) = PNT(J)
PNT(J - 2) = Val: PNT(J - 1) = Val1: PNT(J) = Val2
End If
Else
GoTo 30
End If
30 Next J
Next I
End Select
End Sub '例如对 y坐标排序,x 坐标不动
'调用本子例程时,用call Ssortxyz(pnt,"x")
'""号内字符分别对应x,y,z坐标
'返回排完序后的3维点集
'排列顺序由大到小
Sub OnlySortxyzUP(PNT, XYZ As String)
Dim I, J, K As Integer
Dim Val, Val1, Val2 As Double
Select Case XYZ
Case "x"
For I = LBound(PNT) To UBound(PNT) - 1 Step 3
K = I + 3
For J = K To UBound(PNT) Step 3
If PNT(I - 1) = PNT(J - 1) Then
If PNT(I) < PNT(J) Then
Val = PNT(I): Val1 = PNT(I + 1): Val2 = PNT(I + 2)
PNT(I) = PNT(J): PNT(I + 1) = PNT(J + 1): PNT(I + 2) = PNT(J + 2)
PNT(J) = Val: PNT(J + 1) = Val1: PNT(J + 2) = Val2
End If
Else
GoTo 10
End If
10 Next J
Next I
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Case "y"
For I = LBound(PNT) + 1 To UBound(PNT) - 1 Step 3
K = I + 3
For J = K To UBound(PNT) Step 3
If PNT(I - 1) = PNT(J - 1) Then
If PNT(I) < PNT(J) Then
Val = PNT(I - 1): Val1 = PNT(I): Val2 = PNT(I + 1)
PNT(I - 1) = PNT(J - 1): PNT(I) = PNT(J): PNT(I + 1) = PNT(J + 1)
PNT(J - 1) = Val: PNT(J) = Val1: PNT(J + 1) = Val2
End If
Else
GoTo 20
End If
20 Next J
Next I
'+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Case "z"
For I = LBound(PNT) + 2 To UBound(PNT) - 1 Step 3
K = I + 3
For J = K To UBound(PNT) Step 3
If PNT(I - 1) = PNT(J - 1) Then
If PNT(I) < PNT(J) Then
Val = PNT(I - 2): Val1 = PNT(I - 1): Val2 = PNT(I)
PNT(I - 2) = PNT(J - 2): PNT(I - 1) = PNT(J - 1): PNT(I) = PNT(J)
PNT(J - 2) = Val: PNT(J - 1) = Val1: PNT(J) = Val2
End If
Else
GoTo 30
End If
30 Next J
Next I
End Select
End Sub
Function test()
' Begin the selection
Dim returnObj As AcadObject
Dim basePnt As Variant
On Error Resume Next
' The following example waits for a selection from the user
RETRY:
ThisDrawing.Utility.GetEntity returnObj, basePnt, "Select an object"
If Err <> 0 Then
Err.Clear
MsgBox "Program ended.", , "GetEntity Example"
Exit Function
Else
returnObj.Update
MsgBox "The object type is: " & returnObj.EntityName, , "GetEntity Example"
returnObj.Update
End If
GoTo RETRY
pointUCS = ThisDrawing.Utility.TranslateCoordinates(pointWCS, acWorld, acUCS, False)
End Function |
|