几何尺寸与公差论坛

 找回密码
 注册
查看: 1042|回复: 0

【转帖】initiate a browse for file function

[复制链接]
发表于 2009-4-12 18:42:40 | 显示全部楼层 |阅读模式
initiate a browse for file function
i have a routine that inserts a part into the assembly and creates mates to the reference items in the assembly. that all works great.
the problem is, i can't seem to find the code that allows the user to browse for a file to insert.
i'm sure this is easy, but i just can't seem to find it on my own.
-wes
wes mosier
cswp in '03 & '08
.forefrontstudios.com
2009 premium x64 bit
example from total vb source book
you might also want to check out the getopenfilename function inside solidworks.
i'll get you eh steve, if it's the last thing i dooooo!
josh ... yes
yes, that's exactly the direction i want to go. i just don't think i'm far along enough to know all the syntax yet. i'm pretty sure that i'm on the right track, i just don't know how to get there.
i'm not going to pretend i know much about this yet, so that being said...
this is my first attempt at making a macro and while i'm impressed by my progress today, i'm realizing i might be getting over my head with this. here's what i've got so far, if anyone wants to keep throwing out ideas on what i need to fix, i'd greatly appreciate it.
i want to browse for a file, then insert it into my current assembly, and create these specific mates automatically. that's it... pretty basic. or so i thought. hehe
the mating commands work great, i just can't seem to get it to browse for a file then add that file value to the mate commands.
i don't believe i'm using the getopenfilename format correct. i looked it up in sw api help, but it isn't giving a beginner like me much direction.
here's what i've got...
' ******************************************************************************
' c:\docume~1\wesley\locals~1\temp\swx3484\macro1.swb - macro recorded on 02/20/09 by wesley
' ******************************************************************************
dim swapp as object
dim instance as isldworks
dim part as object
dim boolstatus as boolean
dim longstatus as long, longwarnings as long
dim fn as object
sub main()
set swapp = application.sldworks
set part = swapp.activedoc
set fn = instance.getopenfilename("", "", "", 0, "", "")
boolstatus = part.addcomponent("c:\documents and settings\wesley\desktop\assem4.sldasm", 0.371960453795, 0.1385881313041, -0.2904331929878)
boolstatus = part.extension.selectbyid2("front plane@assem4-20@vessel assem test", "plane", 0, 0, 0, false, 0, nothing, 0)
boolstatus = part.extension.selectbyid2("axis1", "axis", 0, 0, 0, true, 0, nothing, 0)
set part = swapp.activedoc
part.addmate 0, 2, 0, 0.0979002957672, 0
part.clearselection2 true
dim myfeature as object
set myfeature = part.featuremanager.insertfeaturetreefolder2(swfeaturetreefoldertype_e.swfeaturetreefolder_containing)
part.clearselection2 true
boolstatus = part.extension.selectbyid2("front plane@assem4-20@vessel assem test", "plane", 0, 0, 0, false, 0, nothing, 0)
boolstatus = part.extension.selectbyid2("front plane", "plane", 0, 0, 0, true, 0, nothing, 0)
set part = swapp.activedoc
part.addmate 6, 2, 0, 0.06326275636025, 45
part.clearselection2 true
set myfeature = part.featuremanager.insertfeaturetreefolder2(swfeaturetreefoldertype_e.swfeaturetreefolder_containing)
part.clearselection2 true
boolstatus = part.extension.selectbyid2("top plane@assem4-20@vessel assem test", "plane", 0, 0, 0, false, 0, nothing, 0)
answer option explicit
sub main()
dim swapp as sldworks.sldworks
dim swmodel as sldworks.modeldoc2
dim part as sldworks.partdoc
dim assy as sldworks.assemblydoc
dim boolstatus as boolean
dim longwarnings as long, longerror as long
dim swcomp as sldworks.component2
dim refmodel as sldworks.modeldoc2
dim filter as string
dim filename as string
dim fileconfig as string
dim filedispname as string
dim fileoptions as long
dim sdoctype as string
dim swconfig as sldworks.configuration
dim swrootcomp as sldworks.component2
set swapp = application.sldworks
' this following string has three filters associated with it; note the use
' of the | character between filters
filter = "solidworks files (*.sldprt; *.sldasm; *.slddrw)|*.sldprt;*.sldasm;*.slddrw|filter name (*.fil)|*.fil|all files (*.*)|*.*"
filename = swapp.getopenfilename("file to attach", "", filter, fileoptions, fileconfig, filedispname)
' in the dialog, click the down-arrow associated with files to type to see the filters
' dialogfiles of type down arrow key
set swmodel = swapp.activedoc
if swmodel.gettype <> swdocassembly then
msgbox "open assembly first"
exit sub
end if
if ucase(swmodel.getpathname) = ucase(filename) then
msgbox "can't open same file"
exit sub
end if
set assy = swmodel
select case ucase(right(filename, 6))
case "sldasm"
sdoctype = swdocassembly
case "sldprt"
sdoctype = swdocpart
case else
msgbox "wrong type"
exit sub
end select
swapp.documentvisible false, sdoctype
set refmodel = swapp.opendoc6(filename, sdoctype, swopendocoptions_silent, fileconfig, longerror, longwarnings)
set swcomp = assy.addcomponent4(filedispname, fileconfig, 0, 0, 0)
swapp.documentvisible true, sdoctype
set swconfig = swmodel.getactiveconfiguration
set swrootcomp = swconfig.getrootcomponent
boolstatus = assy.extension.selectbyid2("front plane@" & swcomp.name2 & "@" & swrootcomp.name2, "plane", 0, 0, 0, false, 0, nothing, 0)
boolstatus = assy.extension.selectbyid2("axis1", "axis", 0, 0, 0, true, 0, nothing, 0)
assy.addmate 0, 2, 0, 0, 0
assy.clearselection2 true
end sub
wow
that is amazing! it's exactly what i was trying to do, and you just whipped it up for me??!!
thanks!!!!
now i can see that i was pretty far off.
again, thanks a lot!!!
wes mosier
cswp in '03 & '08
.forefrontstudios.com
2009 premium x64 bit
i like the cfiledialog class that ivana inserted above, i did not know it existed. is there a similar class for browsing and selecting just a folder?
dan miel
' example code for modshellfiledisk
'
' to try this example, do the following:
' 1. create a new form
' 2. create a command button named 'cmdbrowsefolder'
' 3. paste all the code from this example to the new form's module.
'*********************************************
' form1.frm
'*********************************************
private sub cmdbrowsefolder_click()
dim strpath as string
' example code for browseforfolder
strpath = browseforfolder( _
me.hwnd, _
"select a folder", _
mcstrexamplepath)
msgbox "the folder you selected is: " & strpath
end sub
'*********************************************
' module : modshellfiledisk
' description : routines for working with the windows
' source : total vb sourcebook 6
'
'*********************************************
option explicit
private const csidl_desktop = &h0
private const bif_returnonlyfsdirs = &h1
private type browseinfo
howner as long
pidlroot as long
pszdisplayname as string
lpsztitle as string
ulflags as long
lpfn as long
lparam as long
limage as long
end type
private declare function shgetpathfromidlist _
lib "shell32.dll" _
alias "shgetpathfromidlista" _
(byval pidl as long, _
byval pszpath as string) _
as long
private declare function shbrowseforfolder _
lib "shell32.dll" _
alias "shbrowseforfoldera" _
(lpbrowseinfo as browseinfo) _
as long
private declare function shgetspecialfolderlocation _
lib "shell32.dll" _
(byval hwndowner as long, _
byval nfolder as long, _
pidl as itemidlist) _
as long
private type shitemid
cb as long
abid as byte
end type
private type itemidlist
mkid as shitemid
end type
public function browseforfolder( _
lnghwnd as long, _
strmessage as string, _
optional strdefault as string) _
as string
' comments : prompts the user for the location of an
' existing directory
' parameters: lnghwnd - handle to window to serve as
' the parent for the dialog. use a form's
' hwnd property for example
' strmessage - prompt message to display on the
' dialog
' strdefault - value to return if the user
' hits 'cancel' to close the dialog
' returns : the path the user selected
' source : total vb sourcebook 6
'
dim bifolder as browseinfo
dim idllist as itemidlist
dim lngidlptr as long
dim lngresult as long
dim strpath as string
on error goto proc_err
' get the location of the user's desktop
shgetspecialfolderlocation lnghwnd, csidl_desktop, idllist
' set browseinfo options
with bifolder
.howner = lnghwnd
.pidlroot = idllist.mkid.cb
.lpsztitle = strmessage
.ulflags = bif_returnonlyfsdirs
end with
' show the browse for folder dialog
lngidlptr = shbrowseforfolder(bifolder)
' get the path indicated in the id list
strpath = space$(260)
lngresult = shgetpathfromidlist( _
byval lngidlptr, byval strpath)
if lngresult <> 0 then
strpath = left$(strpath, instr(1, strpath, vbnullchar) - 1)
else
' user hit 'cancel', use default
strpath = strdefault
end if
browseforfolder = strpath
proc_exit:
exit function
proc_err:
msgbox "error: " & err.number & ". " & err.description, , _
"browseforfolder"
resume proc_exit
end function
edited: 02/24/2009 at 04:57 pm by ivana kolin
thanks ivana
i am swamped at the moment but will be taking a look at this soon.
dan miel
ivana
i am trying the code for the folder browser but i keep getting a error message:
"compile error:method or data member not found" and the me.hwnd is high lighted. do you know what i'm doing wrong?
thanks
dan miel
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|几何尺寸与公差论坛

GMT+8, 2025-1-3 10:40 , Processed in 0.039429 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表