Page 1 of 1

Unable to open SWP generated by MATLAB in Macro

Posted: Mon Jul 05, 2021 10:33 am
by terranan
Hello guys,

I used MATLAB to generate a batch of swp files that help me build models automatically in SolidWorks 2020. But none of them can be opened with SolidWorks Macro. However, if I create a new Macro in SolidWorks and simply paste the script copied from one of the generated swp files and run it, it works. Apparently the scripts are correct but not readable. How can I resolve the issue? Since I have a batch of swp files, it's going to take forever to do copy-and-paste. Any thoughts are appreciated. Thanks.

Re: Unable to open SWP generated by MATLAB in Macro

Posted: Tue Jul 06, 2021 8:10 am
by zwei
Depending on how complex is your macro, you might be able to get it work by saving it as *.SWB instead of SWP, the difference is that SWB

Based on my understanding the SWP file will need to have some encoding done by SOLIDWORKS, hence it could not be generate from other software beside solidworks...

SWB file is different as it is similar to txt file which make it readable and editable outside of SOLIDWORKS, so other software, eg: MATLAB can generate the file.
The downside is that the SWB macro cannot reference any 3rd part library (eg: FSO) and cannot use any userform

Re: Unable to open SWP generated by MATLAB in Macro

Posted: Tue Jul 06, 2021 10:05 am
by terranan
In other words, even if I generate automation scripts, which I capture by macro recording, by MATLAB (Part A), they still need to be manually loaded in SolidWorks (Microsoft Visual Basic for Application) to generate things like reference libraries (Part B)? Seems like I am missing Part B.

When I tried to open swp files by text editors, I got a bunch of messy, partially unreadable lines. Is there a way to read swp outside SolidWorks and use that information to generate a "complete" swp from MATLAB? Thanks.

Re: Unable to open SWP generated by MATLAB in Macro

Posted: Tue Jul 06, 2021 10:07 am
by AlexLachance
terranan wrote: Tue Jul 06, 2021 10:05 am In other words, even if I generate automation scripts, which I capture by macro recording, by MATLAB (Part A), they still need to be manually loaded in SolidWorks (Microsoft Visual Basic for Application) to generate things like reference libraries (Part B)? Seems like I am missing Part B.

When I tried to open swp files by text editors, I got a bunch of messy, partially unreadable lines. Is there a way to read swp outside SolidWorks and use that information to generate a "complete" swp from MATLAB? Thanks.
Change the file extension from .swp to .txt, IMHO it should work.

Re: Unable to open SWP generated by MATLAB in Macro

Posted: Tue Jul 06, 2021 10:43 am
by zwei
terranan wrote: Tue Jul 06, 2021 10:05 am In other words, even if I generate automation scripts, which I capture by macro recording, by MATLAB (Part A), they still need to be manually loaded in SolidWorks (Microsoft Visual Basic for Application) to generate things like reference libraries (Part B)? Seems like I am missing Part B.

When I tried to open swp files by text editors, I got a bunch of messy, partially unreadable lines. Is there a way to read swp outside SolidWorks and use that information to generate a "complete" swp from MATLAB? Thanks.
I am not sure if i understand correctly but...
If you are generating the automation script using matlab, you should write/save it with *.swb extension instead of *.swp.
You will be able to directly run the *.swb in SOLIDWORKS if the macro only use SOLIDWORKS type library.

In fact, when you try edit the *.swb file, SOLIDWORKS will convert it to *.swp with some encoding or something in the background

If you need reference library, I believe you can do it via Late Binding (https://www.codestack.net/solidworks-ap ... ros/types/)
AlexLachance wrote: Tue Jul 06, 2021 10:07 am Change the file extension from .swp to .txt, IMHO it should work.
AFAIK that wont work, *.swp is unlike *.swb file, it could not be read by text editor

Re: Unable to open SWP generated by MATLAB in Macro

Posted: Mon Jul 12, 2021 11:15 am
by terranan
Zhen-Wei Tee wrote: Tue Jul 06, 2021 10:43 am I am not sure if i understand correctly but...
If you are generating the automation script using matlab, you should write/save it with *.swb extension instead of *.swp.
You will be able to directly run the *.swb in SOLIDWORKS if the macro only use SOLIDWORKS type library.

In fact, when you try edit the *.swb file, SOLIDWORKS will convert it to *.swp with some encoding or something in the background

If you need reference library, I believe you can do it via Late Binding (https://www.codestack.net/solidworks-ap ... ros/types/)


AFAIK that wont work, *.swp is unlike *.swb file, it could not be read by text editor
I guess the problem is, I don't know the full content of an actual swp/swb file generated by solidworks. If you try to open an actual swp file, you see some messy and unreadable command lines. Because a portion of the file is not readable (for example, stuff like this "þÿÿÿL þÿÿÿ"), I cannot replicate the macro using matlab, to produce my own swp/swb file. The most I can do is to replicate "the readable portion" (that is the content of a bas file when you export a macro as bas in solidworks; e.g. starting from "Dim swApp As Object" ... to "End Sub"). If I manually copy and paste this readable portion into a new Macro, it is runnable. But that is not what I want. I want my automation script generated by MATLAB to be already an macro recognized by solidworks. Hope this makes sense.

Re: Unable to open SWP generated by MATLAB in Macro

Posted: Mon Jul 12, 2021 11:21 am
by terranan
Zhen-Wei Tee wrote: Tue Jul 06, 2021 10:43 am I am not sure if i understand correctly but...
If you are generating the automation script using matlab, you should write/save it with *.swb extension instead of *.swp.
You will be able to directly run the *.swb in SOLIDWORKS if the macro only use SOLIDWORKS type library.

In fact, when you try edit the *.swb file, SOLIDWORKS will convert it to *.swp with some encoding or something in the background

If you need reference library, I believe you can do it via Late Binding (https://www.codestack.net/solidworks-ap ... ros/types/)


AFAIK that wont work, *.swp is unlike *.swb file, it could not be read by text editor
I saved my automation script as a .swb file and when I imported it into solidworks, solidoworks converted it to a swp, but a message popped up saying "Compile error: user defined type not defined" when I ran it. Any thoughts? Thank you.

Re: Unable to open SWP generated by MATLAB in Macro

Posted: Mon Jul 12, 2021 7:37 pm
by zwei
terranan wrote: Mon Jul 12, 2021 11:21 am I saved my automation script as a .swb file and when I imported it into solidworks, solidoworks converted it to a swp, but a message popped up saying "Compile error: user defined type not defined" when I ran it. Any thoughts? Thank you.
Could you share your swb file?
Without looking at the file it is hard to troubleshoot.
Cheers

Re: Unable to open SWP generated by MATLAB in Macro

Posted: Mon Jul 12, 2021 9:18 pm
by terranan
Zhen-Wei Tee wrote: Mon Jul 12, 2021 7:37 pm Could you share your swb file?
Without looking at the file it is hard to troubleshoot.
Cheers
Here are two files. "Solidworks Macro.swp" is the original macro file recorded by Solidworks. "Solidworks automation.swb" is the automation script that I created from MATLAB to try to replicate the swp. Any thoughts would be appreciated. Thanks.

https://www.dropbox.com/s/rwdgpqb72n9vx ... o.swp?dl=0
https://www.dropbox.com/s/h3ftkjj70jnl8 ... n.swb?dl=0

Re: Unable to open SWP generated by MATLAB in Macro

Posted: Mon Jul 12, 2021 9:37 pm
by josh
My guess is that the swb comes in with no references. Therefore you can't use "Dim swPart as PartDoc", you have to declare everything as a generic Object. Your first couple of declarations (swApp and swDoc) are both Objects.

Re: Unable to open SWP generated by MATLAB in Macro

Posted: Tue Jul 13, 2021 6:36 pm
by zwei
josh wrote: Mon Jul 12, 2021 9:37 pm My guess is that the swb comes in with no references. Therefore you can't use "Dim swPart as PartDoc", you have to declare everything as a generic Object. Your first couple of declarations (swApp and swDoc) are both Objects.
Base on my understanding this should not be the case?
swb should come with basic solidworks reference by default, hence stuff like SldWorks.ModelDoc2 should work...

Re: Unable to open SWP generated by MATLAB in Macro

Posted: Tue Jul 13, 2021 6:55 pm
by zwei
terranan wrote: Mon Jul 12, 2021 9:18 pm Here are two files. "Solidworks Macro.swp" is the original macro file recorded by Solidworks. "Solidworks automation.swb" is the automation script that I created from MATLAB to try to replicate the swp. Any thoughts would be appreciated. Thanks.

https://www.dropbox.com/s/rwdgpqb72n9vx ... o.swp?dl=0
https://www.dropbox.com/s/h3ftkjj70jnl8 ... n.swb?dl=0
I have try to run the *.swp file and notice 2 issue:
1. stp file is not generated
2. document is not closed

I made some edit to the swb file to resolve the issue. See following code
I try running it as swb directly and it is working properly.
Copy the code → paste it in notepad → save as *.swb

Edit: I had also uploaded the *.swb file

Code: Select all

' ******************************************************************************
'Purpose: Automation for 3D ellipse-approximation model construction in SolidWorks
'******************************************************************************

Option Explicit '***

Dim swApp As SldWorks.SldWorks '***
Dim Part As SldWorks.ModelDoc2 '***
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Dim swSheetWidth As Double
Dim swPart As SldWorks.PartDoc '***
Dim myModelView As SldWorks.ModelView '***
Dim swSheetHeight As Double

Dim skSegment As SldWorks.SketchSegment '***
Dim swFeat As SldWorks.Feature
Dim swFeatMgr As SldWorks.FeatureManager

Dim swFeatData As Object
Dim myFeature As Object


Dim partTitle As String 'NEW var

Sub main()
Set swApp = Application.SldWorks
' New Document

swSheetWidth = 0

swSheetHeight = 0
Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2020\templates\Part.prtdot", 0, swSheetWidth, swSheetHeight) '[*]
Set Part = swApp.ActiveDoc

Set swPart = Part
'swApp.ActivateDoc2 "Part10", False, longstatus '***, not required, consider remove?
Set Part = swApp.ActiveDoc

Set myModelView = Part.ActiveView
myModelView.FrameState = swWindowState_e.swWindowMaximized
'insert ext X curve
Part.InsertCurveFileBegin
Part.InsertCurveFilePoint 0.011316, 0#, 0#
Part.InsertCurveFilePoint 0.011343, 0#, 0.00585
Part.InsertCurveFilePoint 0.011366, 0#, 0.0117
Part.InsertCurveFilePoint 0.01139, 0#, 0.01755
Part.InsertCurveFilePoint 0.011407, 0#, 0.0234
Part.InsertCurveFilePoint 0.011406, 0#, 0.02769
Part.InsertCurveFilePoint 0.011406, 0#, 0.030186
Part.InsertCurveFilePoint 0.011394, 0#, 0.032526
Part.InsertCurveFilePoint 0.011379, 0#, 0.034788
Part.InsertCurveFilePoint 0.01137, 0#, 0.036894
Part.InsertCurveFilePoint 0.011362, 0#, 0.038844
Part.InsertCurveFilePoint 0.011369, 0#, 0.040716
Part.InsertCurveFilePoint 0.011378, 0#, 0.042432
Part.InsertCurveFilePoint 0.011394, 0#, 0.044148
Part.InsertCurveFilePoint 0.011392, 0#, 0.04563
Part.InsertCurveFilePoint 0.011418, 0#, 0.047112
Part.InsertCurveFilePoint 0.011439, 0#, 0.048516
Part.InsertCurveFilePoint 0.011465, 0#, 0.049764
Part.InsertCurveFilePoint 0.011487, 0#, 0.051012
Part.InsertCurveFilePoint 0.011546, 0#, 0.052104
Part.InsertCurveFilePoint 0.011531, 0#, 0.053196
Part.InsertCurveFilePoint 0.011491, 0#, 0.05421
Part.InsertCurveFilePoint 0.011448, 0#, 0.055146
Part.InsertCurveFilePoint 0.01145, 0#, 0.056082
Part.InsertCurveFilePoint 0.011514, 0#, 0.057018
Part.InsertCurveFilePoint 0.011542, 0#, 0.058032
Part.InsertCurveFilePoint 0.010607, 0#, 0.059124
Part.InsertCurveFilePoint 0.009938, 0#, 0.060216
Part.InsertCurveFilePoint 0.009883, 0#, 0.061464
Part.InsertCurveFilePoint 0.009859, 0#, 0.062712
Part.InsertCurveFilePoint 0.009839, 0#, 0.064116
Part.InsertCurveFilePoint 0.00991, 0#, 0.065598
Part.InsertCurveFilePoint 0.010107, 0#, 0.06708
Part.InsertCurveFilePoint 0.010225, 0#, 0.068796
Part.InsertCurveFilePoint 0.010287, 0#, 0.070512
Part.InsertCurveFilePoint 0.010333, 0#, 0.072384
Part.InsertCurveFilePoint 0.010392, 0#, 0.074334
Part.InsertCurveFilePoint 0.010435, 0#, 0.07644
Part.InsertCurveFilePoint 0.010486, 0#, 0.078702
Part.InsertCurveFilePoint 0.010544, 0#, 0.081042
Part.InsertCurveFilePoint 0.010595, 0#, 0.083538
Part.InsertCurveFilePoint 0.010641, 0#, 0.087828
Part.InsertCurveFilePoint 0.010683, 0#, 0.093678
Part.InsertCurveFilePoint 0.010722, 0#, 0.099528
Part.InsertCurveFilePoint 0.010769, 0#, 0.105378
Part.InsertCurveFilePoint 0.010829, 0#, 0.111228
Part.InsertCurveFileEnd
'insert ext Y curve
Part.InsertCurveFileBegin
Part.InsertCurveFilePoint 0#, 0.008753, 0#
Part.InsertCurveFilePoint 0#, 0.008753, 0.00585
Part.InsertCurveFilePoint 0#, 0.008762, 0.0117
Part.InsertCurveFilePoint 0#, 0.00876, 0.01755
Part.InsertCurveFilePoint 0#, 0.008793, 0.0234
Part.InsertCurveFilePoint 0#, 0.008831, 0.02769
Part.InsertCurveFilePoint 0#, 0.008842, 0.030186
Part.InsertCurveFilePoint 0#, 0.008849, 0.032526
Part.InsertCurveFilePoint 0#, 0.008853, 0.034788
Part.InsertCurveFilePoint 0#, 0.008843, 0.036894
Part.InsertCurveFilePoint 0#, 0.008831, 0.038844
Part.InsertCurveFilePoint 0#, 0.0088, 0.040716
Part.InsertCurveFilePoint 0#, 0.008759, 0.042432
Part.InsertCurveFilePoint 0#, 0.00871, 0.044148
Part.InsertCurveFilePoint 0#, 0.008658, 0.04563
Part.InsertCurveFilePoint 0#, 0.008606, 0.047112
Part.InsertCurveFilePoint 0#, 0.008552, 0.048516
Part.InsertCurveFilePoint 0#, 0.00852, 0.049764
Part.InsertCurveFilePoint 0#, 0.008469, 0.051012
Part.InsertCurveFilePoint 0#, 0.008435, 0.052104
Part.InsertCurveFilePoint 0#, 0.008396, 0.053196
Part.InsertCurveFilePoint 0#, 0.008412, 0.05421
Part.InsertCurveFilePoint 0#, 0.008506, 0.055146
Part.InsertCurveFilePoint 0#, 0.008624, 0.056082
Part.InsertCurveFilePoint 0#, 0.008747, 0.057018
Part.InsertCurveFilePoint 0#, 0.008739, 0.058032
Part.InsertCurveFilePoint 0#, 0.008114, 0.059124
Part.InsertCurveFilePoint 0#, 0.008076, 0.060216
Part.InsertCurveFilePoint 0#, 0.008193, 0.061464
Part.InsertCurveFilePoint 0#, 0.00836, 0.062712
Part.InsertCurveFilePoint 0#, 0.008533, 0.064116
Part.InsertCurveFilePoint 0#, 0.008545, 0.065598
Part.InsertCurveFilePoint 0#, 0.008577, 0.06708
Part.InsertCurveFilePoint 0#, 0.008565, 0.068796
Part.InsertCurveFilePoint 0#, 0.00852, 0.070512
Part.InsertCurveFilePoint 0#, 0.008482, 0.072384
Part.InsertCurveFilePoint 0#, 0.008446, 0.074334
Part.InsertCurveFilePoint 0#, 0.008432, 0.07644
Part.InsertCurveFilePoint 0#, 0.008442, 0.078702
Part.InsertCurveFilePoint 0#, 0.008462, 0.081042
Part.InsertCurveFilePoint 0#, 0.008485, 0.083538
Part.InsertCurveFilePoint 0#, 0.008537, 0.087828
Part.InsertCurveFilePoint 0#, 0.008584, 0.093678
Part.InsertCurveFilePoint 0#, 0.008641, 0.099528
Part.InsertCurveFilePoint 0#, 0.008677, 0.105378
Part.InsertCurveFilePoint 0#, 0.008703, 0.111228
Part.InsertCurveFileEnd
'insert int X curve
Part.InsertCurveFileBegin
Part.InsertCurveFilePoint 0.010316, 0#, 0#
Part.InsertCurveFilePoint 0.010335, 0#, 0.00585
Part.InsertCurveFilePoint 0.010332, 0#, 0.0117
Part.InsertCurveFilePoint 0.010384, 0#, 0.01755
Part.InsertCurveFilePoint 0.010383, 0#, 0.0234
Part.InsertCurveFilePoint 0.010397, 0#, 0.02769
Part.InsertCurveFilePoint 0.010402, 0#, 0.030186
Part.InsertCurveFilePoint 0.010374, 0#, 0.032526
Part.InsertCurveFilePoint 0.010344, 0#, 0.034788
Part.InsertCurveFilePoint 0.010337, 0#, 0.036894
Part.InsertCurveFilePoint 0.010322, 0#, 0.038844
Part.InsertCurveFilePoint 0.010323, 0#, 0.040716
Part.InsertCurveFilePoint 0.010337, 0#, 0.042432
Part.InsertCurveFilePoint 0.010355, 0#, 0.044148
Part.InsertCurveFilePoint 0.010347, 0#, 0.04563
Part.InsertCurveFilePoint 0.010356, 0#, 0.047112
Part.InsertCurveFilePoint 0.010373, 0#, 0.048516
Part.InsertCurveFilePoint 0.010375, 0#, 0.049764
Part.InsertCurveFilePoint 0.010349, 0#, 0.051012
Part.InsertCurveFilePoint 0.010309, 0#, 0.052104
Part.InsertCurveFilePoint 0.010108, 0#, 0.053196
Part.InsertCurveFilePoint 0.008828, 0#, 0.05421
Part.InsertCurveFilePoint 0.00603, 0#, 0.055146
Part.InsertCurveFilePoint 0.006486, 0#, 0.056082
Part.InsertCurveFilePoint 0.007642, 0#, 0.057018
Part.InsertCurveFilePoint 0.008398, 0#, 0.058032
Part.InsertCurveFilePoint 0.008556, 0#, 0.059124
Part.InsertCurveFilePoint 0.008318, 0#, 0.060216
Part.InsertCurveFilePoint 0.008332, 0#, 0.061464
Part.InsertCurveFilePoint 0.008265, 0#, 0.062712
Part.InsertCurveFilePoint 0.007884, 0#, 0.064116
Part.InsertCurveFilePoint 0.007966, 0#, 0.065598
Part.InsertCurveFilePoint 0.008359, 0#, 0.06708
Part.InsertCurveFilePoint 0.008777, 0#, 0.068796
Part.InsertCurveFilePoint 0.009023, 0#, 0.070512
Part.InsertCurveFilePoint 0.009188, 0#, 0.072384
Part.InsertCurveFilePoint 0.00933, 0#, 0.074334
Part.InsertCurveFilePoint 0.009431, 0#, 0.07644
Part.InsertCurveFilePoint 0.009506, 0#, 0.078702
Part.InsertCurveFilePoint 0.009589, 0#, 0.081042
Part.InsertCurveFilePoint 0.009654, 0#, 0.083538
Part.InsertCurveFilePoint 0.009737, 0#, 0.087828
Part.InsertCurveFilePoint 0.009799, 0#, 0.093678
Part.InsertCurveFilePoint 0.009873, 0#, 0.099528
Part.InsertCurveFilePoint 0.009938, 0#, 0.105378
Part.InsertCurveFilePoint 0.009995, 0#, 0.111228
Part.InsertCurveFileEnd
'insert int Y curve
Part.InsertCurveFileBegin
Part.InsertCurveFilePoint 0#, 0.007753, 0#
Part.InsertCurveFilePoint 0#, 0.007745, 0.00585
Part.InsertCurveFilePoint 0#, 0.007728, 0.0117
Part.InsertCurveFilePoint 0#, 0.007754, 0.01755
Part.InsertCurveFilePoint 0#, 0.007769, 0.0234
Part.InsertCurveFilePoint 0#, 0.007822, 0.02769
Part.InsertCurveFilePoint 0#, 0.007838, 0.030186
Part.InsertCurveFilePoint 0#, 0.007829, 0.032526
Part.InsertCurveFilePoint 0#, 0.007818, 0.034788
Part.InsertCurveFilePoint 0#, 0.00781, 0.036894
Part.InsertCurveFilePoint 0#, 0.00779, 0.038844
Part.InsertCurveFilePoint 0#, 0.007754, 0.040716
Part.InsertCurveFilePoint 0#, 0.007718, 0.042432
Part.InsertCurveFilePoint 0#, 0.007671, 0.044148
Part.InsertCurveFilePoint 0#, 0.007614, 0.04563
Part.InsertCurveFilePoint 0#, 0.007544, 0.047112
Part.InsertCurveFilePoint 0#, 0.007486, 0.048516
Part.InsertCurveFilePoint 0#, 0.00743, 0.049764
Part.InsertCurveFilePoint 0#, 0.007331, 0.051012
Part.InsertCurveFilePoint 0#, 0.007197, 0.052104
Part.InsertCurveFilePoint 0#, 0.006974, 0.053196
Part.InsertCurveFilePoint 0#, 0.00575, 0.05421
Part.InsertCurveFilePoint 0#, 0.003089, 0.055146
Part.InsertCurveFilePoint 0#, 0.00366, 0.056082
Part.InsertCurveFilePoint 0#, 0.004875, 0.057018
Part.InsertCurveFilePoint 0#, 0.005594, 0.058032
Part.InsertCurveFilePoint 0#, 0.006063, 0.059124
Part.InsertCurveFilePoint 0#, 0.006456, 0.060216
Part.InsertCurveFilePoint 0#, 0.006641, 0.061464
Part.InsertCurveFilePoint 0#, 0.006766, 0.062712
Part.InsertCurveFilePoint 0#, 0.006578, 0.064116
Part.InsertCurveFilePoint 0#, 0.006602, 0.065598
Part.InsertCurveFilePoint 0#, 0.006829, 0.06708
Part.InsertCurveFilePoint 0#, 0.007118, 0.068796
Part.InsertCurveFilePoint 0#, 0.007256, 0.070512
Part.InsertCurveFilePoint 0#, 0.007337, 0.072384
Part.InsertCurveFilePoint 0#, 0.007385, 0.074334
Part.InsertCurveFilePoint 0#, 0.007428, 0.07644
Part.InsertCurveFilePoint 0#, 0.007463, 0.078702
Part.InsertCurveFilePoint 0#, 0.007507, 0.081042
Part.InsertCurveFilePoint 0#, 0.007545, 0.083538
Part.InsertCurveFilePoint 0#, 0.007633, 0.087828
Part.InsertCurveFilePoint 0#, 0.0077, 0.093678
Part.InsertCurveFilePoint 0#, 0.007792, 0.099528
Part.InsertCurveFilePoint 0#, 0.007847, 0.105378
Part.InsertCurveFilePoint 0#, 0.00787, 0.111228
Part.InsertCurveFileEnd
'insert the centerline
Part.InsertCurveFileBegin
Part.InsertCurveFilePoint 0#, 0#, 0#
Part.InsertCurveFilePoint 0#, 0#, 0.00585
Part.InsertCurveFilePoint 0#, 0#, 0.0117
Part.InsertCurveFilePoint 0#, 0#, 0.01755
Part.InsertCurveFilePoint 0#, 0#, 0.0234
Part.InsertCurveFilePoint 0#, 0#, 0.02769
Part.InsertCurveFilePoint 0#, 0#, 0.030186
Part.InsertCurveFilePoint 0#, 0#, 0.032526
Part.InsertCurveFilePoint 0#, 0#, 0.034788
Part.InsertCurveFilePoint 0#, 0#, 0.036894
Part.InsertCurveFilePoint 0#, 0#, 0.038844
Part.InsertCurveFilePoint 0#, 0#, 0.040716
Part.InsertCurveFilePoint 0#, 0#, 0.042432
Part.InsertCurveFilePoint 0#, 0#, 0.044148
Part.InsertCurveFilePoint 0#, 0#, 0.04563
Part.InsertCurveFilePoint 0#, 0#, 0.047112
Part.InsertCurveFilePoint 0#, 0#, 0.048516
Part.InsertCurveFilePoint 0#, 0#, 0.049764
Part.InsertCurveFilePoint 0#, 0#, 0.051012
Part.InsertCurveFilePoint 0#, 0#, 0.052104
Part.InsertCurveFilePoint 0#, 0#, 0.053196
Part.InsertCurveFilePoint 0#, 0#, 0.05421
Part.InsertCurveFilePoint 0#, 0#, 0.055146
Part.InsertCurveFilePoint 0#, 0#, 0.056082
Part.InsertCurveFilePoint 0#, 0#, 0.057018
Part.InsertCurveFilePoint 0#, 0#, 0.058032
Part.InsertCurveFilePoint 0#, 0#, 0.059124
Part.InsertCurveFilePoint 0#, 0#, 0.060216
Part.InsertCurveFilePoint 0#, 0#, 0.061464
Part.InsertCurveFilePoint 0#, 0#, 0.062712
Part.InsertCurveFilePoint 0#, 0#, 0.064116
Part.InsertCurveFilePoint 0#, 0#, 0.065598
Part.InsertCurveFilePoint 0#, 0#, 0.06708
Part.InsertCurveFilePoint 0#, 0#, 0.068796
Part.InsertCurveFilePoint 0#, 0#, 0.070512
Part.InsertCurveFilePoint 0#, 0#, 0.072384
Part.InsertCurveFilePoint 0#, 0#, 0.074334
Part.InsertCurveFilePoint 0#, 0#, 0.07644
Part.InsertCurveFilePoint 0#, 0#, 0.078702
Part.InsertCurveFilePoint 0#, 0#, 0.081042
Part.InsertCurveFilePoint 0#, 0#, 0.083538
Part.InsertCurveFilePoint 0#, 0#, 0.087828
Part.InsertCurveFilePoint 0#, 0#, 0.093678
Part.InsertCurveFilePoint 0#, 0#, 0.099528
Part.InsertCurveFilePoint 0#, 0#, 0.105378
Part.InsertCurveFilePoint 0#, 0#, 0.111228
Part.InsertCurveFileEnd
'Make a sketch
Part.Extension.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
Part.SketchManager.InsertSketch True
Part.ClearSelection2 True

'Create ellipse #1
Set skSegment = Part.SketchManager.CreateEllipse(1.61913708406892E-02, 8.17764199252039E-03, 0, 1.73929649008303E-02, 8.17764199252039E-03, 0, 1.61913708406892E-02, 8.77843902259095E-03, 0)
'Make pieces between the ellipse, and ext_x, ext_y, and centerline
Part.Extension.SelectByID2 "Point3", "SKETCHPOINT", Empty, Empty, Empty, False, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve1", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.SketchAddConstraints "sgATPIERCE"
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Point4", "SKETCHPOINT", Empty, Empty, Empty, False, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve2", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.SketchAddConstraints "sgATPIERCE"
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Point2", "SKETCHPOINT", Empty, Empty, Empty, False, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve5", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.SketchAddConstraints "sgATPIERCE"
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Sketch1", "SKETCH", Empty, Empty, Empty, True, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve5", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve1", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve2", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Sketch1", "SKETCH", Empty, Empty, Empty, False, 1, Nothing, 0
Part.Extension.SelectByID2 "Curve5", "REFERENCECURVES", 0, 0, 0, True, 4, Nothing, 0
Part.Extension.SelectByID2 "Curve1", "REFERENCECURVES", 0, 0, 0, True, 2, Nothing, 0
Part.Extension.SelectByID2 "Curve2", "REFERENCECURVES", 0, 0, 0, True, 2, Nothing, 0

Set swFeatMgr = Part.FeatureManager

Set swFeatData = swFeatMgr.CreateDefinition(swFeatureNameID_e.swFmSweep)
swFeatData.AdvancedSmoothing = False
swFeatData.AlignWithEndFaces = 0
swFeatData.AutoSelect = True
swFeatData.D1ReverseTwistDir = False
swFeatData.Direction = 0
swFeatData.EndTangencyType = 0
swFeatData.FeatureScope = True
swFeatData.MaintainTangency = False
swFeatData.Merge = True
swFeatData.MergeSmoothFaces = True
swFeatData.PathAlignmentType = 0
swFeatData.StartTangencyType = 0
swFeatData.ThinFeature = False
swFeatData.ThinWallType = 0
swFeatData.TwistControlType = 0
swFeatData.SetTwistAngle 0
swFeatData.SetWallThickness True, 0
Set swFeat = swFeatMgr.CreateFeature(swFeatData)
'Make a sketch
Part.Extension.SelectByID2 "Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, 0
Part.SketchManager.InsertSketch True
Part.ClearSelection2 True
'Create ellipse #2
Set skSegment = Part.SketchManager.CreateEllipse(2.84511792584823E-02, 0, 0, 3.40285520757959E-02, 0, 0, 2.84511792584823E-02, 2.85476685014174E-03, 0)
'Make pieces between the ellipse, and ext_x, ext_y, and centerline
Part.Extension.SelectByID2 "Point3", "SKETCHPOINT", Empty, Empty, Empty, False, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve3", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.SketchAddConstraints "sgATPIERCE"
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Point4", "SKETCHPOINT", Empty, Empty, Empty, False, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve4", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.SketchAddConstraints "sgATPIERCE"
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Point2", "SKETCHPOINT", Empty, Empty, Empty, False, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve5", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.SketchAddConstraints "sgATPIERCE"
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Sketch2", "SKETCH", Empty, Empty, Empty, True, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve5", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve3", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.Extension.SelectByID2 "Curve4", "REFERENCECURVES", 0, 0, 0, True, 0, Nothing, 0
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Sketch2", "SKETCH", Empty, Empty, Empty, False, 1, Nothing, 0
Part.Extension.SelectByID2 "Curve5", "REFERENCECURVES", 0, 0, 0, True, 4, Nothing, 0
Part.Extension.SelectByID2 "Curve3", "REFERENCECURVES", 0, 0, 0, True, 2, Nothing, 0
Part.Extension.SelectByID2 "Curve4", "REFERENCECURVES", 0, 0, 0, True, 2, Nothing, 0
Set swFeatMgr = Part.FeatureManager
Set swFeatData = swFeatMgr.CreateDefinition(swFeatureNameID_e.swFmSweep)
swFeatData.AdvancedSmoothing = False
swFeatData.AlignWithEndFaces = 0
swFeatData.AutoSelect = True
swFeatData.D1ReverseTwistDir = False
swFeatData.Direction = 0
swFeatData.EndTangencyType = 0
swFeatData.FeatureScope = True
swFeatData.MaintainTangency = False
swFeatData.Merge = True
swFeatData.MergeSmoothFaces = True
swFeatData.PathAlignmentType = 0
swFeatData.StartTangencyType = 0
swFeatData.ThinFeature = False
swFeatData.ThinWallType = 0
swFeatData.TwistControlType = 0
swFeatData.SetTwistAngle 0
swFeatData.SetWallThickness True, 0
Set swFeat = swFeatMgr.CreateFeature(swFeatData)
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Sweep2", "SOLIDBODY", 0, 0, 0, True, 0, Nothing, 0
Part.ClearSelection2 True
Part.Extension.SelectByID2 "Sweep2", "SOLIDBODY", 0, 0, 0, False, 1, Nothing, 0

'Copy the pith body
Set myFeature = Part.FeatureManager.InsertMoveCopyBody2(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, True, 1)
Part.Extension.SelectByID2 "Sweep1", "SOLIDBODY", 0, 0, 0, True, 0, Nothing, 0
Part.Extension.SelectByID2 "Sweep2", "SOLIDBODY", 0, 0, 0, True, 0, Nothing, 0
Part.ClearSelection2 True
'Get the rind body by subtration in Combine feature
Part.Extension.SelectByID2 "Body-Move/Copy1", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0
Part.Extension.SelectByID2 "Sweep1", "SOLIDBODY", 0, 0, 0, True, 1, Nothing, 0
Part.Extension.SelectByID2 "Sweep2", "SOLIDBODY", 0, 0, 0, True, 2, Nothing, 0
Set myFeature = Part.FeatureManager.InsertCombineFeature(15902, Nothing, Nothing)
'Save As
longstatus = Part.SaveAs3("C:\Nan-Wei Desktop\Stalk #423 (refined)\Stalk_423.SLDPRT", 0, 0) '[*]
'Save As
longstatus = Part.SaveAs3("C:\Nan-Wei Desktop\Stalk #423 (refined)\Stalk_423.step", 0, 2) '***, change stp to step '[*]


Part.ClearSelection2 True

partTitle = Part.GetTitle '*** added this line to get title to closedoc
'Close Document
Set swPart = Nothing
Set Part = Nothing
swApp.CloseDoc partTitle '*** update syntax to use part title
msgbox "Macro Ended" '*** Added to indicate marco ended
End Sub
Note:
Line marked with '* * * is updated/added
Line marked with '[*] is hard-coded path
A msgbox is added at the end to indicate macro had ended, remove msgbox "Macro Ended" from the code if it is not required

One thing to highlight is that i noticed you are hard-coding your path/file name as well as the template.
Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2020\templates\Part.prtdot", 0, swSheetWidth, swSheetHeight) '[*]
'Save As
longstatus = Part.SaveAs3("C:\Nan-Wei Desktop\Stalk #423 (refined)\Stalk_423.SLDPRT", 0, 0) '[*]
'Save As
longstatus = Part.SaveAs3("C:\Nan-Wei Desktop\Stalk #423 (refined)\Stalk_423.step", 0, 2) '***, change stp to step '[*]
Because of this the code might not work if:
1. User do not have the template file or SOLIDWORKS 2020
2. User do not have C:\Nan-Wei Desktop\ folder

To make the macro more "flexible", what I would suggest is that:
1. Update the code to use default template specified by user in user system setting by using GetUserPreferenceStringValue method. See http://help.solidworks.com/2017/english ... Value.html
2. Make the Save path "dynnamic", eg: Save it to the same folder as the macro by using GetCurrentMacroPathFolder See https://help.solidworks.com/2019/englis ... older.html

Re: Unable to open SWP generated by MATLAB in Macro

Posted: Thu Jul 15, 2021 8:28 am
by terranan
Zhen-Wei Tee wrote: Tue Jul 13, 2021 6:55 pm I have try to run the *.swp file and notice 2 issue:
1. stp file is not generated
2. document is not closed

I made some edit to the swb file to resolve the issue. See following code
I try running it as swb directly and it is working properly.
Copy the code → paste it in notepad → save as *.swb
Hi Zhen, thanks for your great advice.

I copied your code and saved as .swb. However, after I opened it in SOLIDWORKS, I could not run it since a pop-up message said that "Compile error: user-defined type not defined", and it seemed pointing to the command line "Dim swApp As SldWorks.SldWorks." Do you have any thoughts on that?

Re: Unable to open SWP generated by MATLAB in Macro

Posted: Thu Jul 15, 2021 8:44 am
by zwei
terranan wrote: Thu Jul 15, 2021 8:28 am Hi Zhen, thanks for your great advice.

I copied your code and saved as .swb. However, after I opened it in SOLIDWORKS, I could not run it since a pop-up message said that "Compile error: user-defined type not defined", and it seemed pointing to the command line "Dim swApp As SldWorks.SldWorks." Do you have any thoughts on that?
Weird... It is running flawlessly on my machine...
How do you run the macro?

A. Drag and drop into SOLIDWORKS window
B. Tools→Macro→Run→XXX.swb
C. Tools→Macro→Edit→XXX.swb (XXX.swb got converted to .swp) → Run ... This method will not work as after converting to swp the reference library need to be re-added
D. Other

Re: Unable to open SWP generated by MATLAB in Macro

Posted: Thu Jul 15, 2021 8:50 am
by zwei
Attached is a video of me running the swb file directly.