几何尺寸与公差论坛

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

【转帖】multiple file selection in macro

[复制链接]
发表于 2009-4-13 12:58:22 | 显示全部楼层 |阅读模式
multiple file selection in macro
i have several macros that do various things to drawings (alter file names, save as pdf, change revisions, sign offs etc).  right now i either have the macro process a whole directory, or all open files.  is there a way to use a dialog to select multiple files, and i guess it would return an array of files i can iterate through?
solidworks doesn't have a multi-select "open" dialog.  i think you have two options.  you can use windows api, methodology for which is described in a faq (
although multi-select would be nice, i probably do not need it.. to just be able to fumble around in a directory structure clicking "add" until i have all the files i want, since i might not have them in the same directory.
btw, the macro on that 2nd link doesnt work.  it crashes and says my machine is out of memory and a couple other goofy things.
another option i am entertaining is just writing the code in vb, and accessing sw objects.  but not being a software engineer i am not sure how to do that, (i am guessing i need to "load" the sw library from my vb program, etc).
any links to snippets that might show how this is done?  i can write everything in sw api easily, 'cept the multiple file/add etc setup. so this might be a good project to learn with!
did you look at the code in the second link?  it's really quite simple.  at what point does the macro crash?  if you want to just pick one file at a time, you could use sldworks::getopenfilename.  as far as adding filenames to an array, this is possible.  however, if you want to pick a bunch of files from different directories, i would suggest that you create a form with a list box and some buttons - one for "add", one for "process", one for "cancel", and one for "remove".  the list box can function pretty much like a dynamic array.  you can add items or remove them quite easily in code, with the added benefit of the user being able to visually see the entire list they are creating.
-handleman, cswp (the new, easy test)
attached are a user form and a sample macro using it.  to include this form in a macro you are already using, just import the .frm file in the vba editor.  of course, you'll have to unzip the .frm and the .frx file into the same directory.  the macro file is a quick sample to show how to use the form.  
to add one file at a time, leave the box on the form un-checked.  this will use the solidworks getopenfilename function, which only allows for one file at a time to be selected but it shows the thumbnail preview, etc.  checking the multi-select box will cause sw to create an instance of excel and use the multiple file selection capability of excel's api to select multiple files at once.  all files selected by either method are added to the list on the form.  the code checks for files that are already in the list, so duplicates can't be introduced.  however, there is no error checking for the user simply typing in some name that doesn't exist, so the macro you use will have to check for the possibility of non-existent files.   
-handleman, cswp (the new, easy test)
dim s as string
dim arr() as string
s = dir("c:\temp\*", vbnormal)
redim arr(0)
do while s <> ""
   select case msgbox("add file " & s & " to array?", vbyesnocancel or vbexclamation or vbdefaultbutton1, app.title)
  
  case vbyes
    arr(ubound(arr)) = s
    redim preserve arr(ubound(arr) + 1)
  case vbno
  
  case vbcancel
    exit do
  end select
  s = dir
loop

it actually crashes before it even runs- even trying to edit it i get all these memory errors. very odd.
handle, thanks i just downloaded the file and am going to look at it.
any comments on launching sw functions from a general vb program?
you can't "launch sw functions from a general vb program".  you can use automation and the solidworks api to create or attach to an instance of solidworks and then use the vb program to control that instance.  however, solidworks will still be doing the heavy lifting.  in most cases it's easier to just write all the code in a sw macro.  off the top of my head, reasons to use a standalone .exe controlling sw are:
1. lots of stuff going on that's not particularly related to sw - database stuff, external calculations, etc.  a compiled .exe will run faster, although not noticeably unless you are really doing a lot.
2. more gui stuff available, including file/folder browsing.
3. better ide.
4. more code security if you care about that stuff.  it's harder (although far from impossible) to reverse-engineer compiled code than a .swp!
reasons to use a solidworks macro include:
1. easier to integrate into solidworks - you can make a button or shortcut key for a macro very easily.
2. slightly easier to code, since you are controlling solidworks with solidworks.
3. easy to tweak/improve after you get it working - no recompiling, etc.
4. everything is in one file rather than a collection of files.
-handleman, cswp (the new, easy test)
have a look at lenny's macro propertyeditorspec:
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-12-23 09:38 , Processed in 0.036718 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2023 Discuz! Team.

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