Jump to content

Macro Request - I know nothing about creating these


SuperCAD

Recommended Posts

We have a user here who, despite numerous warnings, continues to keep his models in "hidden lines visible" display style and always saves them this way. Our experience has been that this is NOT the best way to save our models, and we want them to always be shaded with edges.

 

I'd like to know if a macro can be written and coded to the save function so when he saves it will change the display style to shaded with edges BEFORE saving. It would be even better if we could have a message window pop up and warn him about saving parts and assemblies with hidden lines visible.

 

Thank you in advance for any help you can offer.

Link to comment
Share on other sites

Yes and no. No it cant be linked to the save button but you could create and place an api button to save and close while changing the display state.

 

How is this an issue? I mean why is he(or she) against doing it the way the company wants?

Link to comment
Share on other sites

Yes and no. No it cant be linked to the save button but you could create and place an api button to save and close while changing the display state.

 

How is this an issue? I mean why is he(or she) against doing it the way the company wants?

 

You could set it to his save button if you updated it in his CUI...

Link to comment
Share on other sites

You could set it to his save button if you updated it in his CUI...

 

bill, do you know of a way to re "link" the default buttons? I don't think its possible but i also can't say that i have ever tried.

 

SuperCAD, i looked into the Solidworks Task Scheduler for this and you can make a macro and have it run in the Task Scheduler so that it can open his files at night, tweak the Shaded display and resave them. I have a few other things to do tonight but if i get a chance ill work up a simple macro.

Link to comment
Share on other sites

SuperCAD, here is a simple macro you can copy/paste into an empty macro. This takes the current open/active part, sets the display to shaded edges, makes it an iso view, fits to screen, rebuilds, saves and closes. This should work for both parts and/or assemblies. If you want to play around with it comment out the save and close portions of the code by adding ' before it. You can change the view, display etc and then run the code.

 

 

Option Explicit

 

Sub main()

 

Dim swApp As SldWorks.SldWorks

Dim swModel As SldWorks.ModelDoc2

Dim swModView As SldWorks.ModelView

Dim bRet As Boolean

Dim swerror As Long

Dim swwarnings As Long

 

Set swApp = Application.SldWorks ' with SW Api you have to declare the application. When you do this right for all the variables you get the quickFill commands

Set swModel = swApp.ActiveDoc ' this uses the swApp(Solidworks program) active document and sets it as swModel

Set swModView = swModel.ActiveView ' this is in here so you can control the display state of the model.

 

swModView.DisplayMode = swViewDisplayMode_ShadedWithEdges ' sets the display to ShadedWithEdges

 

swModel.ShowNamedView2 "", swIsometricView 'empty string is for the VName, empty string uses ViewId which is the swIsometricView

 

swModel.ViewZoomtofit2 ' fit model to the window

 

swModel.ForceRebuild3 False ' the False is there mainly for assemblies. True rebuilds TopLevel assy only, False rebuilds the top level and sub assemblies. you can have it return a value if the rebuild

' was succesful or not by making a variable for example: Dim RebuildError As Long. then do RebuildError = swModel.ForceRebuild3 (False) this will pass a rebuild True

' or False into the RebuildError for you to do something with.

 

swModel.Save3 swSaveAsOptions_e.swSaveAsOptions_Silent, swerror, swwarnings 'the Save3 is part of IModelDoc2 interface, the stuff in the are enumerations for Options, Errors and warnings.

' with things like this in SW API when you leave off the () you don't return a value, if you want to view or do something with a returned error you would need value = swModel.Save3 (Options, Errors, Warnings)

' you can find this info and what the enumerations are inside the Help menu under API Help topics.

 

swApp.CloseDoc (swApp.ActiveDoc.GetPathName) 'closes the file

 

End Sub

Link to comment
Share on other sites

i also want to note that you can do this with the Task Scheduler. I looked into a bit and you need to do a few custom things to handle it. The code will need a "path" and also some things like opening a document. Right now my code assumes you have a file open and it works with that. You will need a bit of code to work in a directory, open each file, change it, save and close it. There is a sample of this if you go to the SW help file http://help.solidworks.com/2016/English/SolidWorks/sldworks/t_Creating_a_SOLIDWORKS_Macro_Template.htm

 

the sample code works in a directory, opens certain file types(drawings in their case) and saves them as dxf files. notice the $$$TASK_WORKING_DIR$$$. when you place the Token names $$$xxxx$$$ in the code it pops up in the task scheduler and lets you manually pick a directory for instance, or file. You could, with some clever programming i think, make it always go into his files at night and doing this.

 

There are other ways to do this also. If you make a custom macro button using the Save icon and put it on his machine, just tell him when you save before you close to use this save button or something like that :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...