-
Re: How can I find the drawings for parts of an assembly using the API without opening SW?
Simon Turner Aug 26, 2014 9:11 AM (in response to Courtney Milligan)A part file does not contain any reference to the drawing.
You must find all drawing files and check their references to see if they reference the part file.
It is up to you to define which folders to search in. Usually, just the folder containing the part file is sufficient.
-
Re: How can I find the drawings for parts of an assembly using the API without opening SW?
Courtney Milligan Aug 26, 2014 11:38 AM (in response to Simon Turner)I am aware the part or assembly do not contain reference information to the drawing, The Drawing stores the references to the part or assembly. I am looking for an API that basically does the same function as the pack and go (which searches for any items in the current directory that reference the item it was run against), but must happen in the same manner as the one that appears in the right click menu in the Windows Explorer. It must happen without opening Solidworks.
In thinking about it last night though I think it is probably better to write my own function to search the directory for all .slddrw and use the GetAllExternalReferences4. If I find a function provided by SW that performs the pack and go functionality, it will be easier than making my own (which is why I asked), but it will take much longer to run. If I write my own, I only need to query the directory once and write the filename and references to an array/collection. If I use a SW function, it will query the contents of the directory and find that data's dependents every time it is called, which would happen for each sub-component of an assembly. For large assemblies it might query that directory hundreds of times.
-
Re: How can I find the drawings for parts of an assembly using the API without opening SW?
Daniel Andersson Aug 26, 2014 1:50 PM (in response to Courtney Milligan)Have you looked into the Document Manager API? I think that have what you are looking for. I have not used it myself therefore no further reference from me....
-
Re: How can I find the drawings for parts of an assembly using the API without opening SW?
Ivana Kolin Aug 26, 2014 2:03 PM (in response to Courtney Milligan)you are right, but be aware of (bug or feature?) -> when you replace references with documentmanager next time when you use GetAllExternalReferences4 you wil get the same references as before replacing. If you want to see replaced references in documentmanager, you must open and save file in SolidWorks. I am not sure, but I belief that pack-and-go has same issue.
-
-
-
Re: How can I find the drawings for parts of an assembly using the API without opening SW?
Roland Schwarz Aug 26, 2014 2:32 PM (in response to Courtney Milligan)Where-used search tool.
-
Re: How can I find the drawings for parts of an assembly using the API without opening SW?
Courtney Milligan Aug 26, 2014 6:31 PM (in response to Courtney Milligan)Ronald, Interesting application, but I don't need to search the world, just the folder the parent file is in. Could be useful in the future though.
Ivana, I ran into that bug developing a similar application, very annoying. I reported it to SW, but doubt they will ever fix it. I seem to recall there was more to it than it reports the wrong value, but I cannot remember enough. I recall it being with the SwDMDocument16.Save or SaveAs commands. That it was not actually saving the changes. Thanks for the reminder though, I had forgotten about it and will be watching for it.
Turns out it was really easy to just write my own (VB.Net). I have posted portions below for reference. the array DrwRefs will be filled with the drawing filename and the second column will container its references. I can then search it for matches to the dependents found inside the file the user actually opened. It might be more efficient to use a collection here instead of an array, but is not something I have learned yet.
FolderContents = System.IO.Directory.GetFiles(swParentFolder, "*.slddrw")
ReDim Preserve DrwRefs(FolderContents.Length, 20) 'Assumes a maximum of 20 dependents for each drawing
For Each slddrw In FolderContents
swChildren = swGetRefs(slddrw) 'Function that gets the references of any SW file.
For Each Ref In swChildren
DrwRefs(i, j) = Ref
j = j + 1
Next
i = i + 1
Next
Function swGetRefs(sDocFileName As String) As Object
Const sLicenseKey As String = "key as obtained from SW"
Dim swClassFact As SwDMClassFactory
Dim swDocMgr As SwDMApplication3
Dim swDoc As SwDMDocument16
Dim swSearchOpt As SwDMSearchOption
Dim nDocType As Long
Dim nRetVal As Long
Dim vBrokenRefs As Object
Dim vIsVirtuals As Object
Dim vTimeStamps As Object
Dim vDependArr As ObjectvBrokenRefs = Nothing
vIsVirtuals = Nothing
vTimeStamps = Nothing'On Error GoTo GetRefFiles_Error
'==========Determine what type of file=================
Select Case Right(LCase(sDocFileName), 7)
Case ".slddrw"
nDocType = SwDmDocumentType.swDmDocumentDrawing
Case ".sldprt"
nDocType = SwDmDocumentType.swDmDocumentPart
Case ".sldasm"
nDocType = SwDmDocumentType.swDmDocumentAssembly
Case Else
' Not a drawing, assembly or part file,
nDocType = SwDmDocumentType.swDmDocumentUnknown
' so do not open
swGetRefs = "Error"
Exit Function
End Select
'==============Find references==========================
swClassFact = CreateObject("SwDocumentMgr.SwDMClassFactory")
swDocMgr = swClassFact.GetApplication(sLicenseKey)
swSearchOpt = swDocMgr.GetSearchOptionObject
swDoc = swDocMgr.GetDocument(sDocFileName, nDocType, True, nRetVal)If nRetVal = 1 Then
MsgBox("File Missing", vbOKOnly)
swGetRefs = vbNull
End If
vDependArr = swDoc.GetAllExternalReferences4(swSearchOpt, vBrokenRefs, vIsVirtuals, vTimeStamps)
swGetRefs = vDependArr
Exit Function
'GetRefFiles_Error:
'MsgBox "Error " & err.Number & " (" & err.Description & ") in procedure GetRefFiles of Module Module1"
End Function-
Re: How can I find the drawings for parts of an assembly using the API without opening SW?
Ivana Kolin Aug 27, 2014 2:22 AM (in response to Courtney Milligan)"Preserve" is slowing down your application and is in this case overflowing
Beyond that, this is better, than dynamic array
Class SwReference
Property Filename As String
Property Referencename As String
Sub New(filename As String, referencename As String)
Me.Filename = filename
Me.Referencename = referencename
End Sub
End Class
Sub Main()
Dim DrwRefs As New List(SwReference)
For Each slddrw In FolderContents
swChildren = swGetRefs(slddrw) 'Function that gets the references of any SW file.
For Each Ref In swChildren
DrwRefs.Add(New SwReference(slddrw, Ref))
Next
Next
End Sub
-
Re: How can I find the drawings for parts of an assembly using the API without opening SW?
Daniel Andersson Aug 27, 2014 4:36 AM (in response to Ivana Kolin)Why not have Referencename as list since the file may have multiple references.
-
-
Re: How can I find the drawings for parts of an assembly using the API without opening SW?
Roland Schwarz Aug 27, 2014 9:04 AM (in response to Courtney Milligan)Courtney Milligan wrote:
Interesting application, but I don't need to search the world, just the folder the parent file is in.
Sounds like you're using it wrong. The app lets you select which folders to search. No need to "search the world". The app also allows you to choose the file types and reference types to search for.
If you're just trying to find references in the same folder, there are some hacks you can exploit. Pack-and-go will search for references (don't forget the "include drawings" check box). Also, you can do a rename using right-click in a Window folder, which will produce a list of referenced files.
-