VB program to list parts in an Assm?
VB program to list parts in an Assm?
(OP)
Does anyone know of code that will list the components within an assembly without having to open the file?
Thanks
Thanks
When was the last time you drove down the highway without seeing a commercial truck hauling goods?
Download nowINTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS Come Join Us!Are you an
Engineering professional? Join Eng-Tips Forums!
*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail. Posting GuidelinesJobs |
VB program to list parts in an Assm?
|
VB program to list parts in an Assm?VB program to list parts in an Assm?(OP)
Does anyone know of code that will list the components within an assembly without having to open the file?
Thanks Red Flag SubmittedThank you for helping keep Eng-Tips Forums free from inappropriate posts. Reply To This ThreadPosting in the Eng-Tips forums is a member-only feature.Click Here to join Eng-Tips and talk with other members! |
ResourcesWhat is rapid injection molding? For engineers working with tight product design timelines, rapid injection molding can be a critical tool for prototyping and testing functional models. Download Now
The world has changed considerably since the 1980s, when CAD first started displacing drafting tables. Download Now
Prototyping has always been a critical part of product development. Download Now
As the cloud is increasingly adopted for product development, questions remain as to just how cloud software tools compare to on-premise solutions. Download Now
|
RE: VB program to list parts in an Assm?
Importing BOM from .SLDASM into Excel thread559-182457
SW07-SP3.1
SW06-SP5.1
RE: VB program to list parts in an Assm?
It's not possible to view any contents of any file without opening it. I guess you just don't want to open it in SolidWorks?
[/smartass comment]
You can view the external references of a file using the SwDocumentMgr dll. The following code will list the external references of any SW document. However, it only goes one level deep (doesn't list parts of subassemblies), and it will list external references other than parts (such as in-context references to a parent or other assembly).
This stuff isn't exactly straightforward. Depending on which version of SW you have, you may need a license key string from Solidworks API support to get this to work. If you're running SW'07 you should be fine. Any string passed for the key will be ignored. For earlier SW versions you'll need to contact API support.
CODE
Const PATHNAME As String = "Replace this with your file path"
Sub main()
Dim myDocMgrClassFactory As SwDocumentMgr.SwDMClassFactory
Dim myDocMgr As SwDocumentMgr.SwDMApplication
Dim mySwDoc As SwDocumentMgr.SwDMDocument7
Dim extRefs As Variant
Dim i As Long
Dim SrchOptions As SwDocumentMgr.SwDMSearchOption
Dim RefString As String
Dim MsgString As String
Set myDocMgrClassFactory = CreateObject("SwDocumentMgr.SwDMClassFactory")
On Error Resume Next
Set myDocMgr = myDocMgrClassFactory.GetApplication(DOCMGRLICENSEKEYSTRING)
On Error GoTo 0
Set mySwDoc = myDocMgr.GetDocument(PATHNAME, swDmDocumentUnknown, True, Empty)
Set SrchOptions = myDocMgr.GetSearchOptionObject
extRefs = mySwDoc.GetAllExternalReferences(SrchOptions)
RefString = (UBound(extRefs) + 1) & " External References Found:" & vbCrLf
For i = 0 To UBound(extRefs)
RefString = RefString & vbCrLf & extRefs(i)
Next i
If Len(RefString) > 1024 Then
MsgBox Left(RefString, 950) & vbCrLf & vbCrLf & "Number of characters exceeds MsgBox capacity"
Else
MsgBox RefString
End If
End Sub
RE: VB program to list parts in an Assm?
Thanks
RE: VB program to list parts in an Assm?
If you use this code recursively you will need to do some type of circular reference checking. Otherwise you'll overflow the stack. For example, say you have Assembly A and Part B in it. Part B has an in-context reference to Assembly A. Assembly A will show up in Part B's references, and Part B will show up in Assembly A's references. Here is some code that will return an indented reference list by calling this functionality recursively.
CODE
Const PATHNAME As String = "Replace this with your file path"
Sub main()
Dim myDocMgrClassFactory As SwDocumentMgr.SwDMClassFactory
Dim swDocMgr As SwDocumentMgr.SwDMApplication
Dim myDoc As SwDocumentMgr.SwDMDocument7
Dim ParentDoc As SwDocumentMgr.SwDMDocument7
Dim i As Long
Dim RefString As String
Dim MsgString As String
Dim myCollection As Collection
Dim eachRef As Variant
Dim ParentString As String
Set myCollection = New Collection
Set myDocMgrClassFactory = CreateObject("SwDocumentMgr.SwDMClassFactory")
On Error Resume Next
Set swDocMgr = myDocMgrClassFactory.GetApplication(DOCMGRLICENSEKEYSTRING)
On Error GoTo 0
Set myDoc = swDocMgr.GetDocument(PATHNAME, swDmDocumentUnknown, True, Empty)
If Nothing Is myDoc Then
myCollection.Add PATHNAME & " [NOT FOUND]"
Else
myCollection.Add myDoc.FullName
ParentString = myDoc.FullName
GetRefs myCollection, myDoc, swDocMgr, ">", ParentString
End If
''''''''''''''''''''''''
'At this point, myCollection is essentially a 1-dimensional array containing
'structured reference data. For large assemblies this data will not fit
'in the immediate window.
''''''''''''''''''''''''
For Each eachRef In myCollection
Debug.Print eachRef
Next eachRef
End Sub
Sub GetRefs(ByRef AddTo As Collection, ByVal ParentDoc As SwDocumentMgr.SwDMDocument7, ByRef myDocMgr As SwDocumentMgr.SwDMApplication, ByVal NumIndent As String, ByVal Parents As String)
Dim mySwDoc As SwDocumentMgr.SwDMDocument7
Dim SrchOptions As SwDocumentMgr.SwDMSearchOption
Dim extRefs As Variant
Dim i As Long
Set SrchOptions = myDocMgr.GetSearchOptionObject
extRefs = ParentDoc.GetAllExternalReferences(SrchOptions)
If IsEmpty(extRefs) Then
'do nothing
Else
For i = 0 To UBound(extRefs)
Set mySwDoc = myDocMgr.GetDocument(extRefs(i), swDmDocumentUnknown, True, Empty)
If Nothing Is mySwDoc Then
AddTo.Add Left(NumIndent, Len(NumIndent) - 1) & "x" & extRefs(i) & " [NOT FOUND]"
ElseIf InStr(1, Parents, mySwDoc.FullName, vbTextCompare) > 0 Then
'Don't add - circular.
Else
AddTo.Add NumIndent & mySwDoc.FullName
GetRefs AddTo, mySwDoc, myDocMgr, "-" & NumIndent, mySwDoc.FullName & Parents
End If
Next i
End If
End Sub
RE: VB program to list parts in an Assm?
The only thing I've changed at this point is using a multi-dimensional array for parsing out the filenames and displaying those in a listbox (rather than the full paths). I'd like to add some custom property manipulation to this as well such as displaying the description next to the filename/partnumber. I'm not sure how far I can get before I actually have to open the file, but this has been a great help.
Thanks
RE: VB program to list parts in an Assm?
RE: VB program to list parts in an Assm?
Sub GetRefs(ByRef AddTo As Collection, ByVal ParentDoc As SwDocumentMgr.SwDMDocument7, ByRef myDocMgr As SwDocumentMgr.SwDMApplication, ByVal NumIndent As String, ByVal Parents As String)
Matt
CAD Engineer/ECN Analyst
Silicon Valley, CA
http://sw.fcsuper.com/index.php
RE: VB program to list parts in an Assm?
SW07-SP3.1
SW06-SP5.1
RE: VB program to list parts in an Assm?
Did you add the swDocumentMgr.dll reference?
RE: VB program to list parts in an Assm?
Here is the reason for my questions. We have alot of designs that are made up of parts and subs from other designs. Everytime we get an order we print out the entire drawing package (excluding hardware and such). The problem is that with a typical BOM of 80 parts spread throughout 5 directories this takes some serious time and there always seems to be that one drawing that gets missed. The other problem is i end up doing this and taking the heat everytime. I know i should spend more time and make sure its all there but @#it happens. Even if i can get a list of all the refences that would be great. Thanks
RE: VB program to list parts in an Assm?
That said, this will only return referenced parts and assemblies. It will not find drawing files.
RE: VB program to list parts in an Assm?
In an effort to learn, why was a method using the swDocumentMgr.dll employed in this case? Are other methods available to accomplish the tasks in this macro?
Matt
CAD Engineer/ECN Analyst
Silicon Valley, CA
http://sw.fcsuper.com/index.php
RE: VB program to list parts in an Assm?
RE: VB program to list parts in an Assm?
Also if this at least list out the parts then i can identify which parts i need drawing for then either manually print them or set up some custom task or macro.
Thanks
RE: VB program to list parts in an Assm?
This code isn't really a functional macro per se. As-is, it's really just a "stub", if you will, to demonstrate the syntax and usage for the swDocumentMgr dll. Its intended audience is people who are familiar enough with VB/VBA to read it, understand it, and integrate it into their own macros/applications. It does "spit out" some output to the immediate window of the VBA editor, but even that is limited because the immediate window only holds a limited amount of text. It will work if pasted into a blank Excel macro as long as you include the reference to the swDocumentMgr.dll file under Tools->References. Solidworks will not open, either in the background or the foreground.
To save you the trouble of going back and reading my previous posts, I'll answer your other questions again.
The first macro I posted will only get the first level of references - that is, parts or assemblies directly referenced by the file it's run on.
The second macro will get all references as many levels deep as your memory stack will handle.
Neither macro will "spit out" or "list out" anything other than the few lines that fit in the immediate window of the VBA editor.
The license key string must be obtained from SolidWorks API support. It is not your SolidWorks serial number, registration code, purchase order number, zip code, social security number, credit card number, or ATM PIN.
RE: VB program to list parts in an Assm?
Matt
CAD Engineer/ECN Analyst
Silicon Valley, CA
http://sw.fcsuper.com/index.php
RE: VB program to list parts in an Assm?
I am stuck in the middle of this as the 3rd or 4th person to occupy this chair. Scarry thing is that there could be 2 or 3 drawings of a part with the same number in different directories all with different finish callouts. You can imagine the frustration when you get a black part for a green assembly.
I need something that the people ordering parts can work with and possibly print via e-drawings the correct referenced part to the latest revision.
Sorry to ramble and all help and solutions are greatly appreciated. Thanks
RE: VB program to list parts in an Assm?
Seems like a long process but when set-up will do the job.
First i use a macro from Lenny called PropertyEditorGlobal and enter a property called "Location" to all my part files, with a field text of $PRP:"SW-Folder Name"$PRP:"SW-File Name". Of course i need to do this for each directory, but it goes quick.
Then i modify an excel bom template and add the column Location. Now for my assemblies i create a new drawing and add the BOM and bingo parts, quatities and locations.
Now when we run the job at least we can make sure we get all the drawings from the right locations.
Now to figure out how to use task scheduler to run a custom task over and over so once i get all my drawing together i can just print and go.
RE: VB program to list parts in an Assm?
Are your using SolidWorks Professional 2006 sp5.1?
Bradley
SolidWorks Professional x64 2007 SP3.1
Intel(R) Pentium(R) D CPU
3.00 GHz, 3.93 GB of RAM
Virtual memory 12577 MB
NVIDIA Quadro FX 3400
RE: VB program to list parts in an Assm?
I set up a basic VB exe that displays a form prompting the user for an assembly to start from and a new location. The overall goal is to copy and rename the assembly and referneces, but along the way I want to be able to pull specific components out (that I know I am going to modify). I've already written a program to do this via macro inside SW, but it's pretty slow and prone to lock-up.
I discovered that I could do the biggest part of the copying, renaming, and re-referencing without opening SW which makes for a much smaller and robust program. I just needed pointed in the right direction. (thanks Handleman)
Now the last thing I do is open the newly created and referenced files.
RE: VB program to list parts in an Assm?
I am using 06 Office SP3.4. Having some work done on the pc but want to upgrade to 07 after that is done.
RE: VB program to list parts in an Assm?
I am assuming that you are using SolidWorks 2006 Office professional. Why are you not using SolidWorks PDM to create as built or latest versions of your drawings?
Bradley
SolidWorks Premim 2007 x64 SP3.1
PDM Works, Intel(R) Pentium(R) D CPU
3.00 GHz, 4 GB RAM, Virtual memory 12577 MB, nVidia 3400
RE: VB program to list parts in an Assm?
RE: VB program to list parts in an Assm?
I understand what you are saying about a one man shop. I would still setup PDM Works and use if I were the only one using it. There are several advantages, the biggest advantage is that you can pick any revision of any drawing with any date in time and print it as built. You know that revision is the same print as the revision from 7 years ago.
As for people copying the part / drawing and changing stuff like the finish but not the part number is a different issue all together. That is a big problem.
Bradley
SolidWorks Premim 2007 x64 SP3.1
PDM Works, Intel(R) Pentium(R) D CPU
3.00 GHz, 4 GB RAM, Virtual memory 12577 MB, nVidia 3400
RE: VB program to list parts in an Assm?
Matt
CAD Engineer/ECN Analyst
Silicon Valley, CA
http://sw.fcsuper.com/index.php
RE: VB program to list parts in an Assm?
Bradley
SolidWorks Premim 2007 x64 SP3.1
PDM Works, Intel(R) Pentium(R) D CPU
3.00 GHz, 4 GB RAM, Virtual memory 12577 MB, nVidia 3400
RE: VB program to list parts in an Assm?
I have had a request to add the ability to change a specific dimension within an assembly to my program. Before I start down a path, is it possible to edit geometry such as dimensions defining reference plane location from the Document Manager without opening the file or have I reached the limit and must now add that code to first open the file, make the change, and then hand over control to the user?
Thanks to everyone that has helped thus far
RE: VB program to list parts in an Assm?