Hello, I'm looking to write an API program macro that will take two coordinate systems and create a line starting with one column and ending the point of the line with the second column. I don't know how to connect the process with Excel.
Basically I have this
- Coordinate system 1 will be in the columns (x1=A(n),y1=B(n),z1=C(n)) (x2=D(n),y2=E(n),z2=F(n)) Where n is the row.
- I just want Solidworks to do a Dim sk.draw command using Excel-Row1Column 1 start point and then end point Row1 Column 2 then read down both columns until all the lines are plotted.
- Is anyone good at writing code that could help me do something like this?
- It kind of looks like this in Excel.
| Rows | Coordinate System 1-Column 1=A,B,C | ||
|---|---|---|---|
| A-x coordinate | B-y coordinate | C-z coordinate | |
| 1 | 1 | 2 | 3 |
| 2 | ect | ect | ect |

Here's part of a macro I want to modify to use E,D,F
Private Sub btnLines_Click()
Dim swApp As SldWorks.SldWorks
Dim Part As ModelDoc2
Dim i As Integer
Set swApp = CreateObject("SldWorks.Application")
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
If Part Is Nothing Then
MsgBox "Please activate a part document before using this macro."
Exit Sub
End If
If Part.GetType <> 1 Then
MsgBox "Please activate a part document before using this macro."
Exit Sub
End If
If Not Part.GetActiveSketch2 Is Nothing Then
MsgBox "Please exit the sketch before running this macro."
Exit Sub
End If
Part.ClearSelection2 True
Part.Insert3DSketch
Part.SetAddToDB True
i = 2
While Range("A" & i).Value <> ""
Part.createline2 Range("A" & i - 1).Value / 1000, Range("B" & i - 1).Value / 1000, Range("C" & i - 1).Value / 1000, _
Range("A" & i).Value / 1000, Range("B" & i).Value / 1000, Range("C" & i).Value / 1000
i = i + 1
Wend
Part.SetAddToDB False
Part.ClearSelection
Part.Insert3DSketch
Part.ClearSelection2 True
End Sub
Welcome to SOLIDWORKS forums Alex.
Check the macro here by Ivana here : https://forum.solidworks.com/message/428537#428537
You might also find this post helpful for your future reference.
Didn't help really. That's just for points. I don't know how to do it for lines.
I just need to figure out how to change the lines from going straight down 1 column and instead going from A1-D1 B1-E1 and C1-F1
You mean that start point for XYZ will be in the cells A, 1; B,2; C,3 and end point for XYZ will be in cells D,1; E,1; F,1 and then for next line the row would move down by + 1. If yes then you can simply use:
So how do I implement that into the code. I have no coding experience and when I replace it with the i=1 part to Wend, I get a ton of errors. In this one, I get Wend without While Compiling error
Private Sub btnLines_Click()
Dim swApp As SldWorks.SldWorks
Dim Part As ModelDoc2
Dim i As Integer
Set swApp = CreateObject("SldWorks.Application")
Set swApp = CreateObject("SldWorks.Application")
Set Part = swApp.ActiveDoc
If Part Is Nothing Then
MsgBox "Please activate a part document before using this macro."
Exit Sub
End If
If Part.GetType <> 1 Then
MsgBox "Please activate a part document before using this macro."
Exit Sub
End If
If Not Part.GetActiveSketch2 Is Nothing Then
MsgBox "Please exit the sketch before running this macro."
Exit Sub
End If
Part.ClearSelection2 True
Part.Insert3DSketch
Part.SetAddToDB True
i = 1
Do While exSheet.Cells(i, 1).Value <> ""
xpt = exSheet.Cells(i, 1).Value
ypt = exSheet.Cells(i, 2).Value
zpt = exSheet.Cells(i, 3).Value
xpt1 = exSheet.Cells(i, 4).Value
ypt1 = exSheet.Cells(i, 5).Value
zpt1 = exSheet.Cells(i, 6).Value
swModel.CreateLine2 xpt, ypt, zpt, xpt1, ypt1, zpt1
i = i + 1
Wend
Part.SetAddToDB False
Part.ClearSelection
Part.Insert3DSketch
Part.ClearSelection2 True
End Sub
Also, I'm getting running errors now everytime I try to run the excel document, it keeps saying I need an active part document open, but I have solidworks open in a part document. Why is this an error?
Try this macro but make sure you've an excel file open with the required values.
Make sure your macro references are in order.
https://forum.solidworks.com/message/390051#390051