|
how get an assembly name
i don´t know how to get an assembly name. i know that for components i have to write like this: name = swchildcomp.name2() but what about for assemblies?
thanks for your help!
edited: 02/18/2009 at 03:12 am by marian ruiz
hi marian,
you can use modeldoc2::gettitle
have a look in the api help there are several examples that display this.
cheers,,
--stav.
in this world i am nobody...
and nobody is perfect ;) !!!
---------
solidworks office 2008 sp4.0
dell precision pws390
nvidia quadro fx 3450/4000 sdi
strip path and extension from modeldoc2: getpathname
gettitle was here in combination with solidpdm unreliable
thank you for your help!
i have done this macro to get the solidworks document's name:
sub main()
dim swapp as sldworks.sldworks
dim swmodel as sldworks.modeldoc2
set swapp = createobject("sldworks.application")
set swmodel = swapp.activedoc
dim nameas string
name = swmodel.gettitle()
name = left(name, instr(name, ".") - 1)
end sub
i think that it is the shortest!
slightly shorter:
sub main()
name = application.sldworks.activedoc.gettitle()
name = left(name, instr(name, ".") - 1)
end sub
note that this method (both your code above and this shorter version) is unreliable, though, and will not work in every case. it is permissible to have multiple "." in a filename. if your filename is "boltm6x1.0thread.sldprt" then this code will give you "boltm6x1".
also, gettitle follows your windows explorer setting for "hide extensions for known file types". if this option is checked in windows, gettitle will return the name with no extension and this code will fail.
i'll get you eh steve, if it's the last thing i dooooo!
edited: 02/18/2009 at 08:06 am by josh brady
josh highlighted a valid issue there.
a possible sollution can be splitting the title wherever there exists a "." character and checking the resulting strings and to be more precise the last string if it is the extension string.
essentially solidworks documents only use specific extesnions (for an assembly it will be .sldasm)
this can also be adjasted for any solidworks document by changing the extension string you are looking for.
so in code what i mean would be the following:
dim vresarray as variant
dim arraybound as integer
dim titlestr as string, asmtitle as string
.
.
.
titlestr = "a.title.with.several.dots.sldasm" 'or whatever title you get
vresarray = split(titlestr, ".")
arraybound = ubound(vresarray)
if arraybound = 0 then
msgbox "no extension"
asmtitle = titlestr
else
if vresarray(arraybound) = "sldasm" then
msgbox "assembly extension found"
asmtitle = left(titlestr, len(titlestr) - 7)
else
asmtitle = titlestr
end if
end if
msgbox asmtitle
in this world i am nobody...
and nobody is perfect ;) !!!
---------
solidworks office 2008 sp4.0
dell precision pws390
nvidia quadro fx 3450/4000 sdi
edited: 02/18/2009 at 09:05 am by stavros antoniou
how about using josh's but changing the left statment
sub main()
name = application.sldworks.activedoc.gettitle()
name = left(name, len(name) - 7)
end sub
i'm editing this. i just realized that if there is not a period in the title which is caused by your explorer settings not showing the file extension that this would probably error and/or give the wrong result.
dan miel
edited: 02/18/2009 at 10:23 am by dan miel
originally posted by: dan miel
how about using josh's but changing the left statment
sub main()
name = application.sldworks.activedoc.gettitle()
name = left(name, len(name) - 7)
end sub
dan miel
you could but if the document does not have an extension (maybe because it was hidden) then the return string will be erroneous.
--stav.
in this world i am nobody...
and nobody is perfect ;) !!!
---------
solidworks office 2008 sp4.0
dell precision pws390
nvidia quadro fx 3450/4000 sdi
rather than wrestling with the "hide extension for known file types", i prefer to parse the entire path. this can be had from modeldoc2.getpathname. it will always include the extension. filenames may not contain a "\", so you can easily get the filename with extension by splitting with "\" as a separator. then you can either split again with "." and rebuild the name or you can find the position of the last "." using instrrev. or you can just assume that the extension has 6 characters and strip off the last 7 per dan's post.
i'll get you eh steve, if it's the last thing i dooooo!
model.getpathname fails with new not saved assembly
and what about this one using "\":
sub main ()
dim a as string
dim atext() as string, s as string
dim name as string
dim swapp as sldworks.sldworks
dim swmodel as sldworks.modeldoc2
set swapp = createobject("sldworks.application")
set swmodel = swapp.activedoc
a = swmodel.getpathname
atext = split(a, "\")
s = atext(ubound(atext))
name = left(s, instr(s, ".") - 1) 'name=name of the modeldoc2
end sub |
|