Hi there,
I´m trying to create a code snippet that detects a derived part in an assembly. The first part was pretty easy to achieve.
I´m checking all documents in the assembly for Referenced Documents. If the Referenced Documents count is larger then 0 it is a derived part.
Here is what I got so for:
Sub Main() oInvDoc = ThisDoc.Document Dim refDocs As DocumentsEnumerator = oInvDoc.AllReferencedDocuments Dim refDoc As Document For Each refDoc In refDocs Dim DerivDoc As Document If refDoc.ReferencedDocuments.Count > 0 Then DerivDoc = refDoc.ReferencedDocuments.Item(1) MessageBox.Show(refDoc.DisplayName & " Ist abgeleitete Komponente!!") MessageBox.Show(DerivDoc.FullFileName) End If Next End Sub
But if the link to the base part is suppressed it won´t detect a referenced document, obviously. So I looked up the programming help under General concepts -> File and Document References and there is an object Document Descriptor that has the property ReferenceSupressed which gives back a boolean. That looks like exactly like what I need but for the life of me I can´t figure out how I have to instantiate that object.
I tried to use it with the referenced document (refDoc) but couldn´t get it to work.
Some help here would be very appreciated.
All the best
Falk
Solved! Go to Solution.
Solved by philippe.leefsma. Go to Solution.
Solved by philippe.leefsma. Go to Solution.
Hi Falk,
You can retrieve this info using Document.ReferencedDocumentDescriptors.Item(idx).ReferenceSuppressed.
Below is a screenshot of the VBA debugger in which I have a derived part open with link to source suppressed.
Regards,
Philippe.
I managed to wrap my head around this and I was doing something essentially wrong.
I´m iterating through all referenced documents in the assembly and tried to access the object ReferencedDocumentDescriptors on each document, but if there is no reference from the document in the assembly to another document the DocumentDescriptor.ReferenceSuppressed object doesn´t exist.
So as a result this is my code:
Format:HTML Format
Version:1.0
StartHTML: 165
EndHTML: 8866
StartFragment: 314
EndFragment: 8834
StartSelection: 314
EndSelection: 314
SubMain()
oInvDoc = ThisDoc.Document
Dim refDocs As DocumentsEnumerator = oInvDoc.AllReferencedDocuments
Dim refDoc As Document
Dim DerivDoc As Document
ForEach refDoc In refDocs
Try
If refDoc.ReferencedDocumentDescriptors.Item(1).ReferenceSuppressed = True Then
MessageBox.Show(refDoc.DisplayName &" has suppressed references !!")
EndIf
Catch
MessageBox.Show(refDoc.DisplayName &" has no supressed references!!")
EndTry
If refDoc.ReferencedDocuments.Count > 0 Then
DerivDoc = refDoc.ReferencedDocuments.Item(1)
MessageBox.Show(refDoc.DisplayName &" Is derived Part!!")
'MessageBox.Show(DerivDoc.FullFileName)
EndIf
Next
End Sub
Now I just need to know one more thing, how can I reactivate a supressed link in a derived component?
Thanks in advance
Falk
Dim doc As Document Set doc = ThisApplication.ActiveDocument Dim derivedPartComp As DerivedPartComponent Set derivedPartComp = doc.ComponentDefinition.ReferenceComponents.DerivedPartComponents(1) derivedPartComp.SuppressLinkToFile = False
______________________________________________________________
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
Cool Phillipe,
exactly what I needed.
I apologize for my coding n00bness but I´m not familiar with the VBA Editor and its debugging capabilities.
I know in theory how it works.
I started coding with VB Studio Express 2010 recently and still have a lot to learn.
But the coding I´m doing right now is all in iLogic rules and forms.
It´s way more easier for me right now to get the automation done then having to deal with all the details of getting
an Inventor plugin going.
Thank you very much for your support
All the best
Falk
Another question surfaced in the process. Now that I reenabled the suppressed link to the base part for the derived parts I need to copy that part as well (Iwant to copy all components of the assembly). But when I instantiate the DocumentsEnumerator at the beginning these references are not there.
Is there a way to add a document reference to the DocumentsEnumerator?
Thanks
Sorry, I don't understand the question. Do you have a sample code and more details that illustrate what you ar etrying to do?
Basically you cannot modify directly the references of a document, it is done as a result of other operations. Did you try "FileDescriptor.ReplaceReference" to swap the referenced file with another?
Regards,
Philippe.
Hello Philippe,
I didn´t posted all the details of my little project. What I´m trying to do is basically a customized copy function for design templates. What I´m doing is using the DocumentsEnumerator of the assembly with AllReferencedDocuments to catch all document references.
When there are derived parts in the assembly with suppressed links they are not in the Enumeration. So I wasn´t able to catch the document references of these suppressed base parts.
I´m cycling through all Referenced Documents in one rule. When I put in the code for activating suppressed links somewhere in that loop it will do that but they will not be added to the enumeration (obviously).
So I created another rule which is called before I start the loop and instantiation of the enumerator and that did the trick.
Actually I wanted to have all the code in one rule but I guess I´m running into limitations here either on my skills or iLogic.
Here is the whole code (without the ActivateSuppressedReference Rule):
Sub Main() iLogicVb.RunExternalRule("ActivateSupressedReference") Dim Path As String = ThisDoc.WorkspacePath & "\" Dim oldPath As String = ThisDoc.Path & "\" 'MessageBox.Show(Path, "Workspace-Pfad") 'MessageBox.Show(oldPath, "alter Workspace-Pfad") oInvDoc = ThisDoc.Document Dim refDocs As DocumentsEnumerator = oInvDoc.AllReferencedDocuments Dim refDoc As Document For Each refDoc In refDocs Dim oldDisplayName As String = refDoc.DisplayName 'read filename with file extension Dim Filename_Ext As String = refDoc.FullFileName.Substring(oldpath.length) 'get the extension of the file Dim strFileExtension As String = Filename_Ext.substring(Filename_Ext.Length-4,4) 'get the filename without extension from the filename with extension Dim Filename As String = Filename_Ext.substring(0,Filename_Ext.Length-4) 'detect If document Is Assembly And save As Assembly If refDoc.DocumentType = 12291 Then 'define new Name for copy of the referenced document NewFileName = InputBox("Geben Sie den neuen Namen für die Baugruppe " & Filename_Ext &" ein!","Neuer Dateiname", Filename) 'check For blank filename And Set To DisplayName If NewFileName = "" Then NewFileName = Filename End If 'check For extension In name And remove If there If NewFileName.EndsWith(".iam") = True Then NewFileName = NewFileName.Substring(0,NewFileName.length-4) 'MessageBox.Show(NewFileName) End If refDoc.SaveAs(Path & NewFileName & ".iam",False) refDoc.DisplayName = oldDisplayName Call CopyDrawing(Path, oldPath, Filename_Ext, NewFileName) Else 'initialize object for PartComponentDefinition to access property if part is member of content center Dim oPartCompDef As PartComponentDefinition oPartCompDef = refDoc.ComponentDefinition If Not oPartCompDef.IsContentMember Then NewFileName = InputBox("Geben Sie den neuen Namen für das Bauteil " & Filename &" ein!","Neuer Dateiname", Filename) 'check For blank filename If NewFileName = "" Then NewFileName = Filename End If 'check For extension In name And remove If there If NewFileName.EndsWith(".ipt") = True Then NewFileName = NewFileName.substring(0,NewFileName.length-4) 'MessageBox.Show(NewFileName) End If refDoc.SaveAs(Path & NewFileName & ".ipt",False) refDoc.DisplayName = oldDisplayName Call CopyDrawing(Path, oldPath, Filename_Ext, NewFileName) Else 'MessageBox.Show("Die Komponente " & refDoc.DisplayName & "ist ein Content Center Part und wird nicht kopiert !") End If End If Next NewAssemblyName = InputBox("Wie lautet der neue Name für die Haupt-Baugruppe?", "Neuer Baugruppenname", ThisDoc.FileName(False)) If NewAssemblyName = "" Then NewAssemblyName = ThisDoc.FileName(False) Dim oldAssemblyName = ThisDoc.FileName(True) ThisDoc.Document.SaveAs(Path & NewAssemblyName & ".iam",False) 'ThisDoc.Document.DisplayName = oldAssDisplayName Call CopyDrawing(Path, oldPath, oldAssemblyName, NewAssemblyName) Else If NewAssemblyName.EndsWith(".iam") = True Then NewAssemblyName = NewAssemblyName.Substring(0,NewAssemblyName.length-4) End If Dim oldAssemblyName = ThisDoc.FileName(True) ThisDoc.Document.SaveAs(Path & NewAssemblyName & ".iam",False) 'ThisDoc.Document.DisplayName = oldAssDisplayName Call CopyDrawing(Path, oldPath, oldAssemblyName, NewAssemblyName) MessageBox.Show("Main Assembly kopiert") End If 'Dim ctrl As ControlDefinition 'ctrl = ThisApplication.CommandManager.ControlDefinitions("AppSaveAllCmd") 'ctrl.Execute() End Sub Private Sub CopyDrawing (ByVal NewPath As String, ByVal oldPath As String, ByVal oldName As String, ByVal NewName As String) Dim oDestinationDoc As DrawingDocument Dim strFileExtension As String = oldName.substring(oldName.Length-4,4) oldName = oldName.substring(0,oldName.Length-4) Dim sFileName As String = oldPath & oldname & ".idw" Try If System.IO.File.Exists(sFileName) Then oDestinationDoc = ThisApplication.Documents.Open(sFileName) oDestinationDoc.SaveAs(NewPath & NewName & ".idw",False) Dim oDocDescriptor As DocumentDescriptor oDocDescriptor = oDestinationDoc.ReferencedDocumentDescriptors.Item(1) Dim oFileDescriptor As FileDescriptor oFileDescriptor = oDocDescriptor.ReferencedFileDescriptor oFileDescriptor.ReplaceReference(NewPath & NewName & strFileExtension) oDestinationDoc.Update() oDestinationDoc.Close End If Catch oDestinationDoc.Close MessageBox.Show("error") End Try End Sub Private Sub ActivateSupRef(ByVal oDoc As Document) Dim oDerivDoc As Document Try If oDoc.ReferencedDocumentDescriptors.Item(1).ReferenceSuppressed Then oDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents(1).SuppressLinkToFile = False oDerivDoc = oDoc.ReferencedDocuments.Item(1) End If Catch End Try 'If oDoc.ReferencedDocuments.Count > 0 Then 'oDerivDoc = oDoc.ReferencedDocuments.Item(1) 'MessageBox.Show(oDerivDoc.DisplayName & " is Basepart for derived Part "& oDoc.DisplayName &" !") 'End If End Sub
The code is a bit garbled up due to testing and trying different approaches but I hope it´s still readable.
I thought it might be possible after activating the suppressed references to add them to the enumeration so the base parts for the derived parts going through the same loop as all the other components.
Cheers Falk
Were you able to follow my thoughts on the document enumerator?
Anyways, I guess it wouldn´t had been a problem to just introduce a new one after enabling the broken link even in one rule, maybe with a sub.
Cheers
Sorry, the last update is not clear. So to sum it up are you saying that after unsupressing the link in the derived part, the references are not getting updated automatically? Did you try to manually update the document (doc.Update) or a rebuild maybe (doc.Rebuild)? If it still doesn'tappear, I would think this is an unexpected behavior.