Page 1 of 1

Export Macro Not Outputting Apperances

Posted: Fri Aug 25, 2023 11:09 am
by tk421
Below is my work in progress of my exporting macro. It works well but does not output the colors that are on the model. it's not a huge deal, but it would be nice to be able to have those come into PowerMill how they left SW. Any help is appreciated. Thanks!

Code: Select all

Option Explicit

' Connect to SolidWorks & access the
' libraries needed
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swComp As SldWorks.Component2
Dim longStatus As Long

Sub main()
     ' Connect to active document
     Set swApp = Application.SldWorks
     Set swModel = swApp.ActiveDoc
     Set swSelMgr = swModel.SelectionManager
     
     ' Check to see if the active document is an assy;
     ' if it isnt, exit the macro.
     If Not TypeOf swModel Is SldWorks.AssemblyDoc Then
          swApp.SendMsgToUser2 "This macro can only be run in an assembly document", 4, 2
          Exit Sub
     End If
     
     ' Get the selected part or sub-assy as 'swComp'
     Set swComp = swSelMgr.GetSelectedObjectsComponent4(1, -1)
     
     ' Grab the wanted information from the selected part
     ' From the assy name, get the job number only
     ' & add the " - " to the name
     Dim partName As String
     Dim partPath As String
     Dim myval As String
     Dim nameForExport As String
     Dim fullExportPath As String
     
     nameForExport = Left(swModel.GetTitle, 5) & " - "
     
     ' Get the part name and the part path to build th
     ' directory name for exporting
     partName = Replace(swComp.Name, "-1", "")
     partPath = swComp.GetPathName
     
     ' Modify the path for the nc toolpaths folder
     partPath = Replace(Replace(partPath, "SW Design", "NC Toolpaths"), partName & ".SLDPRT", "")
     fullExportPath = partPath & nameForExport & "Stock.step"
     
     ' Save out the file as a step
     swApp.SetUserPreferenceToggle swUserPreferenceToggle_e.swStepExportAppearances, True
     longStatus = swModel.SaveAs3(fullExportPath, 0, 2)
     
     ' Flash message to user that this operation is done
     swApp.SendMsgToUser "Done"
End Sub

Re: Export Macro Not Outputting Apperances

Posted: Fri Aug 25, 2023 11:51 am
by AlexB
I believe you'll need to specify that this should be AP214 vs AP203 for your STEP file. If I recall correctly, only 214 maintains color selections.

https://help.solidworks.com/2021/englis ... ptions.htm
image.png

Re: Export Macro Not Outputting Apperances

Posted: Fri Aug 25, 2023 1:11 pm
by tk421
wow there's a lot of stuff to sift through in this api. i wouldve never found that. thanks a bundle bro!

Re: Export Macro Not Outputting Apperances

Posted: Thu Oct 12, 2023 11:58 am
by CarrieIves
I'm not figuring out how to specify the STEP format in my save as macro.

We go thru a bunch of stuff in my macro to set the name with the revision and the correct file path. Here's where we set up fullfilename and then our save command.

fullfilename = FileLocation & sFileName & "_Rev" & sRev & "_" & sStatus & draftletter & ".STEP"

swModel.SaveAs (fullfilename)

FileLocation is the network drive path based on the directory the model is in. sRev and sStatus and draftletter are reading custom properties. sFilename is the model filename stripped of the extension and path.

I think I'm supposed to use a different SaveAs API as I read the help, but I'm not clear on using it, nor am I clear on what to do to get the STEP AP214 into my macro.

Can I get some guidance here?

Re: Export Macro Not Outputting Apperances

Posted: Thu Oct 12, 2023 12:09 pm
by josh
Do you mean 203 vs 214, or are you saying that you're unsuccessful actually saving out a file at all?

If the first one, you have to use SetUserPreference[something] per the posts above prior to the SaveAs command.

If the second one (you can't save a file at all), please add a debug.print statement to tell you the value of fullfilename ahead of the saveas statement so that you can verify that the string you're building is good. For example, I note that you have "FileLocation & sFileName".... There is no backslash explicitly added here. If FileLocation does not end with a backslash, your save will fail (or at least save somewhere you don't expect).

Re: Export Macro Not Outputting Apperances

Posted: Thu Oct 12, 2023 1:33 pm
by CarrieIves
We are successfully saving right now and it is STEP 203.

I added these two lines:

Dim stepversion As Long
stepversion = swApp.SetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swStepAP, 214)

Now it is saving in STEP 214.

Thanks.