|
參考:附 .XLS 及 .SWP 檔
Excel批點坐標作點線或曲線.rar
(112.18 KB, 下载次数: 30)
2018-6-11 22:20重傳
- ' ******************************************************************************
- ' macro recorded on 01/25/14 by scliang
- ' 連結 excel作點,連成不規則曲線(放樣曲線)或直線
- ' 版本 V140125-2-170916
- ' 操作:
- ' 1. 開 Excel座標檔,副檔名為 .XLS
- ' 2. 開 SW 新零件
- ' 3. 執行 [url=https://www.swbbsc.com/forum-57-1.html]宏[/url] main()
- '
- ' ******************************************************************************
- Dim swApp As Object
- Dim Part As Object
- Dim boolstatus As Boolean
- Sub Draw() '作圖
- Set swApp = Application.SldWorks
- Set Part = swApp.ActiveDoc
- On Error Resume Next
- If Part Is Nothing Then
- MsgBox "請先打開或者新建SolidWorks Part"
- Exit Sub
- End If
- With UserForm1
- If .OptionButton1.Value = True Then item = 1
- If .OptionButton2.Value = True Then item = 2
- If .OptionButton3.Value = True Then item = 3
- End With
- Dim skSegment As Object
- Dim pointArray As Variant
- Dim points() As Double
- Set xl = GetObject(, "Excel.Application") '連結Excel
- '*** Get active sheet in Exce
- Set xls = xl.ActiveSheet
- '*** Get value in Excel cell value
- rgs = InputBox("鍵入Excel儲存格(單元格)範圍", , "C11:E203") 'L11:N15
- Set wb = xl.Activeworkbook
- If wb Is Nothing Then
- MsgBox "請先打開 Excel 點座標檔"
- Exit Sub
- End If
- Part.SketchManager.Insert3DSketch True '插入3D草圖
- Start_Row = xls.range(rgs).ROW
- u = Right(rgs, Len(rgs) - InStr(rgs, ":")) '取 : 之後的單元格
- End_Row = xls.range(u).ROW
- Set rags = xls.range(rgs) 'Excel數據範圍
- ct = (End_Row - Start_Row + 1) * 3 '數據總筆數數
- ReDim points(1 To ct) As Double
- n = 0
- For Each rag In rags 'Excel單元格數據
- If rag = "" Then Exit For
- n = n + 1
- points(n) = rag / 1000 '數據存至陣列
- Next
- Select Case item
- Case 1 '作點
- For i = 1 To n Step 3
- Set skSegment = Part.SketchManager.CreatePoint(points(i), points(i + 1), points(i + 2))
- myModelView.RotateAboutCenter 0, 0
- Next
-
- Case 2 '不規則曲線連點
- pointArray = points
- Set skSegment = Part.SketchManager.CreateSpline((pointArray))
- myModelView.RotateAboutCenter 0, 0
- Case 3 '直線連點
- m = n - 5
- For i = 1 To m Step 3
- Set skSegment = Part.SketchManager.CreateLine(points(i), points(i + 1), points(i + 2), points(i + 3), points(i + 4), points(i + 5))
- myModelView.RotateAboutCenter 0, 0
- Next
- End Select
- End Sub
- Sub main() '主程式
- UserForm1.Show 0
- End Sub
复制代码 |
|