• This forum is the machine-generated translation of www.cad3d.it/forum1 - the Italian design community. Several terms are not translated correctly.

macro pack and go

  • Thread starter Thread starter diego106
  • Start date Start date

diego106

Guest
good evening to all users of the forum. It's the first time I wrote in the forum and I wanted to ask you for help in the "macro pack and go" of the solidworks program.
I've been using it for about a year, but I have some gaps about it.
I built a macro helping me with the internet, more precisely I documented myself on the site of solidworks at the following link I modified the macro because I need to copy and rename all the files present in a solidworks project so that I can delete and then rewrite the first characters of the file names. as long as it comes to parts, assemblings, drawings the macro works, while when it comes to text files with equations that are recalled in the parts or assembling the macro does not work. In practice I would like to understand how you can replace the external text file of the renamed equations, with the previous one in the parts or assemblies.
Thank you in advance.
 
guardando le api per il pack and go noto la possibilità di copiare e aggiornare i link anche dei file esterni delle equazioni. sotto ti riporto la macro di esempio che trovi nel manuale, spero ti sia di aiuto.


pack and go part and linked equation example (vb.net)
this example shows how to determine if a part document includes any equations and whether those equations are linked files. the example also shows how to add the part document and any linked equations to pack and go.

'----------------------------------------
' preconditions:
' 1. verify that the specified part and equation documents
' exist.
' 2. verify that c:\temp exists.
' 3. open the immediate window.
'
' postconditions:
' 1. opens the specified part document.
' 2. adds the part and linked equation documents
' to pack and go and copies the files to
' c:\temp.
' 3. to verify, examine c:\temp.
'
' note: because the model is used elsewhere,
' do not save changes.
'-----------------------------------------
imports solidworks.interop.sldworks
imports solidworks.interop.swconst
imports system
imports system.diagnostics

partial class solidworksmacro

public sub main()

dim swmodel as modeldoc2
dim swmodeldocext as modeldocextension
dim sweqnmgr as equationmgr
dim swpackandgo as packandgo
dim filename as string
dim errors as integer
dim warnings as integer
dim i as integer
dim ncount as integer
dim eqnlinked as boolean
dim pgfilenames as object = nothing
dim status as boolean
dim statuses as object
dim mypath as string


filename = "c:\users\public\documents\solidworks\solidworks 2018\samples\tutorial\api\microphonehousing.sldprt"
swmodel = swapp.opendoc6(filename, swdocumenttypes_e.swdocpart, swopendocoptions_e.swopendocoptions_silent, "", errors, warnings)
swmodeldocext = swmodel.extensiondebug.print("file = " & sw model.getpathname)
debug.print(" ')

' get estimation manager object
sweqnmgr = swmodel.getequationmgr

' list the equations, get whether they're linked
' to files, make sure any equations are up to date,
' and get the paths where they're stored
ncount = sweqnmgr.getcount
for i = 0 to ncount - 1
debug.print(" equation #" & i & " = " & sweqnmgr.equation(i))

eqnlinked = sweqnmgr.linktofile
debug.print(" equation linked to file? " & eqnlinked)

if eqnlinked then

'make sure equations are up to date
sweqnmgr.updatevaluesfromexternalequationfile()

'get path and name of linked equation file
debug.print(" path and file name of linked equation: " & sweqnmgr.filepath)

end if
next i

debug.print(" ")

' get pack and go object
debug.print(" pack and go")
swpackandgo = swmodeldocext.Getting Packandgo

' get current paths and names of the documents
status = swpackandgo.Getdocumentnames(pgfilenames)
debug.print(" add these solidworks files to pack and go: ")
if not pgfilenames is nothing then
for i = 0 to ubound(pgfilenames)
debug.print(" the file to pack up is: " & pgfilenames(i))
next i
end if

' set document paths and names for pack and go
status = swpackandgo.setdocumentavetonames(pgfilenames)

' override path where to save documents
mypath = "c:\temp\"
status = swpackandgo.stsavetoname(true, mypath)

' pack and go both solidworks and non-solidworks files
statuses = swmodeldocext.savepackandgo(swpackandgo)

end sub

''' <summary>
''' the sldworks swapp variable is pre-assigned for you.
''' </summary>
public swapp as sldworks


end class
 
Thanks for the help jenuary. I looked at the example but unfortunately I can't take advantage of it.
from what I understood you do not change the path of the equation file inside the part file.
I still attach the macro that I did, so I can try it when you have availability.
towards the end I entered the instruction "if my extention=txt.then goto 15" because only by canceling the text file equations, the macro works and replaces the first 2 characters of the names of each part, together or drawing; If not, if I comment the "if" instruction, copy the same names in the folder without changing anything.
I apologize if the code I created is not done very well.. thanks anyway for the availability.

private sub commandbutton1_click()
'sub packandgo()
dim swapp as sldworks.sldworks
dim swmodel as sldworks.modeldoc2
dim swmodeldocext as sldworks.modeldocextension
dim swpackandgo as sldworks.packandgo
dim openfilepath as string
dim openfilename as string
dim savepath as string
dim savename, v3, v7, v8, v9, v10, v11, v13, v14 as string
dim myfilename as string
dim myextension as string
v1, v2, v4, v5, v6, v12, v15 as variant
dim pgfilenames as variant
dim pgfilestatus as variant
dim status as boolean
dim statuses as variant
dim i as long

v15 = 2 'n° of letters to be removed to the name

openfilepath = "c:\users\user\desktop\complesivo tramoggia.sldasm"
savepath = "c:\users\user\desktop\temp\"


set swapp = application.sldworks
set swmodel = swapp.opendoc(openfilepath, swdocassembly)
'set swmodel = swapp.activedoc
openfilepath = swmodel.getpathname
openfilename = mid(openfilepath, instrrev(openfilepath, "\") + 1, instrrev(openfilepath, ".") - instrrev(openfilepath, "\") - 1)

set swmodeldocext = swmodel.extension

'get pack and go object
set swpackandgo = swmodeldocext.getpackandgo

'includes any drawings
swpackandgo. includedrawings = true

status = swpackandgo.getdocumentnames(v1)

v2 = instrrev(v1(i), "\") + 1 'n° of famines up to the last slash
v3 = mid(v1(i), v2) 'I press the string to begin with n°v2 characters
v4 = len(v1(i))) I count the number of total characters
v5 = len(v3) 'count the number of characters of the striga v3
v6 = v4 - v5'n° of characters before part of the percrso
v7 = mid(v1(i), 1, v6)'s first part route


'set folder where to save the files
status = swpackandgo.setsavetoname(true, savepath)




'get files path
status = swpackandgo.getdocumentsavetonames(pgfilenames, pgfilestatus)
for i = 0 to ubound(pgfilenames)
myfilename = mid(pgfilenames(i), instrrev(pgfilenames(i), "\") + 1, instrrev(pgfilenames(i), ".") - instrrev(pgfilenames(i), "\") - 1)
myextension = right(pgfilenames(i), len(pgfilenames(i) - instrrev(pgfilenames(i), ".") + 1)

if myextension = ".txt" then pgfilenames(i) = ""
if myextension = ".txt" then goto n15

v11 = len(v1(i))) 'I count the number of current string total characters
v9 = len(myfilename) 'n° characters name
v16 = right(myfilename, v9 - v15) 'stringa remaining characters of the name
v10 = len(myextension) 'n° file extension characters
v8 = v11 - v6 - v9 - v10 'n° characters to be taken only if > of zero
v12 = mid(v1(i), v6, v8)
if v8 = 0 then goto n10
v13 = right(v12, v8 - 1)
v14 = v13 + "\"
N10:

pgfilenames(i) = savepath & v14 & "55" & v16 & myextension

'debug.print & pgfilenames(s)
n15:
next

'set files path
status = swpackandgo.setdocumentsavetonames(pgfilenames)

'
'pack and go
statuses = swmodeldocext.savepackandgo(swpackandgo)

end
 
hi diego106, you should attach a small file of test set with attachment also the files of equations, so do the tests on something concrete.
just something simple, possibly in rel.2021
 
hi jenuary, below I'll tie you a set to try a macro. But I have the solidworks 2019 version, I hope it's not a problem.
thanks for the courtesy
 

Attachments

hi diego106,
I did a test and saw that following the macro code, the pack and go keeps the connection to the file, copy it to the destination folder along with the axieme and below parts.
if I change the text file from the outside and save it, changing its values, known that in the axieme or in the part nothing happens.
I had to open the equation mask, remove and re-check the link to the file and everything went to good end.
I don't know if it's a bug or there's a bee that allows the update from the outside, I haven't looked yet.
 
hello jenuary, in the meantime thank you for the availability.
I wanted to understand one thing first: in the folder where the macro copies the files, the latter, are renamed by replacing the first two characters of the name with 55?
for example the name "frame" should become "55laio".
to me this does not work; only works if I insert codes:

if myextension = ".txt" then pgfilenames(i) = ""
if myextension = ".txt" then goto n15

Therefore I delete the equation file from the pack & go and obviously the equation file is not copied and renamed in the folder
 
hi diego, I haven't tried yet, the macro I had attached to you didn't rename the parts, but he made a copy of it in the project by taking to the equation files.
try to change the macro that I brought you with the cut code in the name and see if it behaves as you wish. in case you let me know that I help you
 
Hello, jenuary. I tried to change the macro that you wrote to me following your advice, but the problem persists and is equal to the problem that I had in my macro or I can not make a copy of my project with pack and go changing the file names, the problem is always the equation file because the pack and go does not accept it if I try to change the name; instead, a file starts together or drawing si. When you can, without haste, try to check with the macro and the axieme I sent you. take into account that the macro should replace the first 2 characters of the file name with 55, as I had written to you on the 26th of the week. thanks in advance for the help
 
Hello, jenuary. I tried to change the macro that you wrote to me following your advice, but the problem persists and is equal to the problem that I had in my macro or I can not make a copy of my project with pack and go changing the file names, the problem is always the equation file because the pack and go does not accept it if I try to change the name; instead, a file starts together or drawing si. When you can, without haste, try to check with the macro and the axieme I sent you. take into account that the macro should replace the first 2 characters of the file name with 55, as I had written to you on the 26th of the week. thanks in advance for the help
hi I noticed something, probably it's a solidworks kiss.. .
if you make a pack and go of a set with equation bonds from the open axieme then take away also the text file and update it. if instead the pack and go is done from the outside, i.e. with closed set and launches the pack and go from windows explorer with right click above the file, the txt file of the equations, even if the check is active is not copied
 

Forum statistics

Threads
44,997
Messages
339,767
Members
4
Latest member
ibt

Members online

No members online now.
Back
Top