Open and save using task scheduler

Anyone know how to set up a macro so the task scheduler can open a file and then save it.  Not to convert, just save.

Thanks.

SolidworksTask Scheduler
Answers 39
DG
DG 2010-11-09 Best Answer

You may use this macro Zoom and Rebuild

OR

You need to create SolidWorks Macro Template to use in  task scheduler (Search for SolidWorks Macro Template under task scheduler help files)

MC
MC 2010-11-09

Link is bad?

Manually found it on the website.  Now, how do you use it?  I can see the custom task in the task scheduler, but how do you point it to a specific folder?

MC
MC 2010-11-09

Well I am having no luck....

I am a "real novice" when it comes to this kind of stuff.  Seems as though the macro file has a lot of pointless characters in it when I try to view it in notepad.

DG
DG 2010-11-10

Sorry for the bad link, I have fixed it now.

You just need to edit the macro as change the file location. And then finally run it though SW (no need to use task scheduler).

Extract the macro to a known location. Then check these linkf for how to

Edit a Macro

Run a Macro

And if you need to go via task scheduler route, you need to create a SolidWorks Macro Template

MC
MC 2010-11-10

I am starting to get it now.....

But when I run it, I get this....

DG
DG 2010-11-10

Good to know that you're getting into it.

Now to fix this issue, edit your macro (using the method I stated above) and go to Tools > References. Now uncheck any missing library and select the corresponding one for the SW version you have. Click OK to apply the changes and then click on Save to save the settings. Run the macro again (make sure you have changed the file location)

MC
MC 2010-11-10

I think that I am close.....

Is this where you change the folder location??  Capture

And with these check marks it still errors out.   Capture2

Here is the error.  Capture3

Will this macro work as is with 2010?  Or does it need modified?

Final Help?

DG
DG 2010-11-10

Don't know why that is not working. Try the attached one, just extract and run.

Currently the macro works for SW Drawings. Change the codes for parts or assemblies. And no need to set the folder/file location in the macro, just use browse option.

MC
MC 2010-11-10

Works like a champ!!  Thanks a million.

Now if Solidworks can just figure out why the assm thumbnails got messed up in the first place.

DG
DG 2010-11-10

It happens some time so a system restart might fix it

MC
MC 2010-11-10

For some odd reason - the macro is erroring out after opeing precisely 128 files....

Duplicated it on 2 completely different folders.

Thoughts???

DG
DG 2010-11-10

It can be because of the system resources. Not sure how to fix it or how about doing the files in order of 100 files per run.

MC
MC 2010-11-10

Can you specify in the macro what file to start with?  Or a range?

If the folder I want to update has more than 128 files I do not know how to make it start at that point.

DG
DG 2010-11-10

Yes a range can be set. And to further use the macro, create another folder inside that folder and move the updated files into that folder. Now run the macro again to update the remaining files else it might keep on running on same files.

Give me some time to look at range thing.

MC
MC 2010-11-10

Cool - thank you.

I just did another folder and it stops at exactly 128 files.

My system resources were fine during the process.  Is there maybe a "ticker" or "counter" that is getting tripped and ending the process??

I added the error captures.

DG
DG 2010-11-10

I have never updated that much number of files so not sure of reason for the error but here is the one with counter. It'll stop at 100 files

MC
MC 2010-11-10

I edited my above message with captures -- any help?

I attempted to set the number to 200 on your new macro and it still stopped at 128.

Anyway to "type in" the starting file number and then it will go 128 from that one??

DG
DG 2010-11-10
<p>Michael Cudnik wrote:</p><p></p><p>I attempted to set the number to 200 on your new macro and it still stopped at 128.</p><p></p>

I said the macro stops at 128 (not sure why) in macro and that's why I have put the counter to 100. You may put 128 if you wish.

<p>Michael Cudnik wrote:</p><p></p><p>Anyway to "type in" the starting file number and then it will go 128 from that one??</p>

I don't know any option to do that. May be create another folder in the same location and move the updated files in that. After that rerun the macro.

FV
FV 2010-11-10

Hi Michael,

I didn't have time to test this, but I suspect the problem was from the MAX_PATH constant set to 260 characters...

You must have a massive folder structure behind these assemblies you're trying to run, and by coincidence, it's always been the 128th assembly...

Try this macro out...and report back...

Hope this helps

Cheers

FV
FV 2010-11-10

Please disregard my previous post...as that max_path constant was strictly for the search folder...

I'm assuming you trying to achieve the same you asked in your other thread....so go with the macro I wrote up instead...

As for what's wrong with this macro, my next guess would be, that the Function Dir that's being used to get the file list, must have a limitation in characters...if you still wish to use this macro...i would recommend moving your assemblies a couple of levels (of folders) down...

Here's the solution...

https://forum.solidworks.com/message/190147#190147

cheers

MC
MC 2010-11-10

I have to hit OK button after every assembly closes

?????

FV
FV 2010-11-10

Using which macro?

SF
SF 2010-12-06

Deepak,

Is it possible to add the function of the macro below into your "Batch File Open and Save" macro, so that it can perform the original function and the additional function found on my macro below?

' ******************************************************************************
' Created and Copyright by Raúl Rosa (08/02/2010) - All rights Reserved
' ******************************************************************************
Dim swApp As Object

Dim Part As Object

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc

Dim MyProp(1, 1) As String

'Property names
MyProp(0, 0) = "ECCN"
MyProp(0, 1) = "Export Control Flag"

'Property values
MyProp(1, 0) = ""
MyProp(1, 1) = "EXPORT CONTROLLED: READ THE DESIGN BINDER"

Dim m As Integer

For m = 0 To 1
    retval = Part.AddCustomInfo3("", MyProp(0, m), 30, _
         MyProp(1, m))
Next m

Set SelMgr = Part.SelectionManager
boolstatus = Part.Extension.SelectByID2("Design Binder", "DOCSFOLDER", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.InsertAttachment("J:\SolidWorks 2009 Settings\Macros\Export Control\Add Export Control Metadata\EXPORT CONTROLLED.doc", False)
boolstatus = Part.Extension.InsertAttachment("J:\SolidWorks 2009 Settings\Macros\Export Control\Add Export Control Metadata\PROPRIETARY NOTICE.doc", False)

Dim longstatus As Long, longwarnings As Long

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2("", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
Dim myComment As Object
Set myComment = Part.Extension.AddComment("EXPORT CONTROLED")
Part.ClearSelection2 True

End Sub

-----------------------------------------------------------------------------------------------------------------------------------------

Thanks in advance for any help.

Raul

DG
DG 2010-12-06

Yes it should be doable

SF
SF 2010-12-09

Can you help with this?

I have tried, but cannot get it to work.

TIA,


Raul

DG
DG 2010-12-09

Raul I have almost done it, just getting issues with adding properties but will fix them soon

SF
SF 2010-12-09

Wow, that was fast. Thank you much.

I eagerly await.

Thanks again,


Raul

DG
DG 2010-12-10

Here you go

SF
SF 2010-12-16

Deepak,

You are the best. Thank you so much. Works great.

Raul

SF
SF 2010-12-16

Ok, I promise this will be the last request:

Can you add the "Add comment macro" below you did for me to this?

--------------------------------------------------------------------

Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Part.DeleteCustomInfo ("Test") ' Will delete the existing custom property "Test" if exists
Part.AddCustomInfo3 "", "Test", 30, "testing12" 'Will add custom property "Test"
 
Dim myNote As Object
Dim myAnnotation As Object
Dim myTextFormat As Object
Set myNote = Part.InsertNote("$PRP:""Test""") ' Change setting here for font
If Not myNote Is Nothing Then
   myNote.Angle = 0
   boolstatus = myNote.SetBalloon(0, 0)
   Set myAnnotation = myNote.GetAnnotation()
   If Not myAnnotation Is Nothing Then
 
     longstatus = myAnnotation.SetLeader3(swLeaderStyle_e.swNO_LEADER, 0, True, False, False, False)
      boolstatus = myAnnotation.SetPosition(0#, 0#, 0#) ' Set the position of note to 0, 0, 0
      boolstatus = myAnnotation.SetTextFormat(0, False, myTextFormat)
   
   End If
End If
Part.ClearSelection2 True
Part.WindowRedraw
End Sub
---------------------------------------------------------------------------------------------------------
I tried to add it, but not working out too good.
See below:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
' Batch File Open and Save -------------------------------12/10/10
' Macro will find all the part files in the specified Path/location, add listed custom properties, comment and design binder documents.
' Finally rebuild & save the files.
' Macro edited as per requirements from Raul on SolidWorks forums: https://forum.solidworks.com/message/194653#194653
' Currently the macro works for SW Parts. Change the codes for assemblies.
' The macro works on 128 files in one run. For more you will have to re run the macro.
' Please backup your files and use at own risk.
' ------------------------------------------------------------------------------
' Written by: Deepak Gupta (http://gupta9665.wordpress.com/)
' ------------------------------------------------------------------------------
Option Explicit
Dim myNote          As Object
Dim myAnnotation    As Object
Dim myTextFormat    As Object
Dim swApp           As SldWorks.SldWorks
Dim swModel         As SldWorks.ModelDoc
Dim selMgr          As SelectionMgr
Dim sFileName       As String
Dim Path            As String
Dim MyProp(1, 1)    As String
Dim m               As Integer
Dim nErrors         As Long
Dim nWarnings       As Long
 
Sub main()
    Set swApp = Application.SldWorks
      
    Path = BrowseFolder("Select a Path/Folder")
   
    Path = Path + "\"
   
  
    sFileName = Dir(Path & "*.sldprt")
     
    Do Until sFileName = ""
 
    Set swModel = swApp.OpenDoc6(Path + sFileName, swDocPART, swOpenDocOptions_Silent, "", nErrors, nWarnings)
    Set swModel = swApp.ActiveDoc
   
   
'Property names
MyProp(0, 0) = "ECCN"
MyProp(0, 1) = "Export Control Flag"
'Property values
MyProp(1, 0) = ""
MyProp(1, 1) = "EXPORT CONTROLLED: READ THE DESIGN BINDER"
For m = 0 To 1
    swModel.AddCustomInfo3 "", MyProp(0, m), 30, MyProp(1, m) ' Will not update/edit the properties if they exist.
        
Next m

Set selMgr = swModel.SelectionManager
swModel.Extension.SelectByID2 "Design Binder", "DOCSFOLDER", 0, 0, 0, False, 0, Nothing, 0
swModel.Extension.InsertAttachment "J:\SolidWorks 2009 Settings\Macros\Export Control\Add Export Control Metadata\EXPORT CONTROLLED.doc", False
swModel.Extension.InsertAttachment "J:\SolidWorks 2009 Settings\Macros\Export Control\Add Export Control Metadata\PROPRIETARY NOTICE.doc", False
swModel.Extension.SelectByID2 "", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0
Dim myComment As Object
Set myComment = swModel.Extension.AddComment("EXPORT CONTROLED")

swModel.DeleteCustomInfo ("EXPORT CONTROL FLAG") ' Will delete the existing custom property "EXPORT CONTROL FLAG" if exists
swModel.AddCustomInfo3 "", "EXPORT CONTROL FLAG", 30, "EXPORT CONTROLLED: READ THE DESIGN BINDER" 'Will add custom property "EXPORT CONTROL FLAG"
   
Dim myNote As Object
Dim myAnnotation As Object
Dim myTextFormat As Object
Set myNote = Part.InsertNote("$PRP:""EXPORT CONTROL FLAG""") ' Change setting here for font
If Not myNote Is Nothing Then
   myNote.Angle = 0
   boolstatus = myNote.SetBalloon(0, 0)
   Set myAnnotation = myNote.GetAnnotation()
   If Not myAnnotation Is Nothing Then
  
     longstatus = myAnnotation.SetLeader3(swLeaderStyle_e.swNO_LEADER, 0, True, False, False, False)
      boolstatus = myAnnotation.SetPosition(0#, 0#, 0#) ' Set the position of note to 0, 0, 0
      boolstatus = myAnnotation.SetTextFormat(0, False, myTextFormat)     
   End If
End If
part.ClearSelection2 True
part.WindowRedraw
         
    swModel.EditRebuild3
  
    swModel.Save2 False
   
   swApp.CloseDoc swModel.GetTitle
  
   Set swModel = Nothing
  
   sFileName = Dir
  
Loop
End Sub

-----------------------------------------------------------------------------------------------------------------------------------------------
Thanks again for all your help in this effort. With this macro, you will be saving me hours of work.
R.
DG
DG 2010-12-16

Here you go

Happy to help.. so no worries if you have more to ask

SF
SF 2010-12-16

Fantastic. Thanks a million.

SF
SF 2010-12-16

Ok, spoke too soon. One last request.

Can you add the follwoing to the macro as well?

------------------------------------------------------------------------------

Dim swApp As Object
Dim swPart As SldWorks.ModelDoc2
Const PicPath As String = "\\SW_Settings\Macros\background.tif"
Sub main()

Set swApp = Application.SldWorks

Set swPart = swApp.ActiveDoc
If swPart.SceneBkgImageFileName = "" Then
    swPart.InsertBkgImage PicPath
Else
    swPart.DeleteBkgImage
End If

End Sub

-------------------------------------------------------------------------------------

Thanks again.

R.

DG
DG 2010-12-16

Can you put that picture to test the macro

SF
SF 2010-12-17

see attached.

Thanks

DG
DG 2010-12-17

Just add these lines above swModel.EditRebuild3 in the macro.

<div id="_mcePaste">boolstatus = swApp.SetUserPreferenceIntegerValue(swUserPreferenceIntegerValue_e.swColorsBackgroundAppearance, swColorsBackgroundAppearance_e.swColorsBackgroundAppearance_Plain)</div><div id="_mcePaste"> </div><div id="_mcePaste"> </div><div id="_mcePaste">Const PicPath As String = "\\SW_Settings\Macros\background.tif"</div><div id="_mcePaste"> </div><div id="_mcePaste"> </div><div id="_mcePaste">If swModel.SceneBkgImageFileName = "" Then</div><div id="_mcePaste">    swModel.InsertBkgImage PicPath</div><div id="_mcePaste">Else</div><div id="_mcePaste">    swModel.DeleteBkgImage</div><div id="_mcePaste">End If</div>
SF
SF 2010-12-29

Thanks for everything.

DG
DG 2010-12-29

Just note that after adding the background file, file size will increase.

SF
SF 2010-12-29

Yes. and being that only umcompressed tif seem to be supported, I may not go this route, as it does increase the file size tremendowsly.


R.