Ref doc only if not set to reference on BOM

Ref doc only if not set to reference on BOM

darrell.wcd
Enthusiast Enthusiast
675 Views
7 Replies
Message 1 of 8

Ref doc only if not set to reference on BOM

darrell.wcd
Enthusiast
Enthusiast

I have this bit of code I found and modified.

It will create a .pdf of all the oRefDoc in the current assembly, as well as create a .stp file of the model, but it will also open all the oRefDoc that are set to Reference.

 

I also need it to skip all the files that are in a sub assembly that is marked Reference. 

 

Capture.PNG

I think if it would only look at parts only and structured section of the BOM would work.

but it might not get all the sub assemblies that way.

 

If anyone has a suggestion or some sample code, or even better yet would be a revised code, would be great.

 

Sub Main()
If ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject Then
    MessageBox.Show("Please run this rule from an assembly file.", "WCD Drawing Helps")
    Exit Sub
	
	docmodel = ThisDoc.ModelDocument
	'docmodel.Rebuild()
	
	
End If
	Dim oAsmDoc As AssemblyDocument
    oAsmDoc = ThisApplication.ActiveDocument
	Dim oOptions As NameValueMap
	Dim oRefDocs As DocumentsEnumerator
    oRefDocs = oAsmDoc.AllReferencedDocuments
    Dim oRefDoc As Document
    numFiles = 0	
	Dim Process As String
	Dim PDFAddIn As TranslatorAddIn
	Dim oContext As TranslationContext
	'Dim oOptions As NameValueMap
	Dim oDataMedium As DataMedium
	Call ConfigurePDFAddinSettings(PDFAddIn, oContext, oOptions, oDataMedium)
	
	Dim oAModelPN As String
	oAModelPN = oAsmDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").ItemByPropId(5).Value
	
	
	
	 


Dim oFolder As String = "C:\Inventor PDF" 'The target folder for export
If Not System.IO.Directory.Exists(oFolder) Then System.IO.Directory.CreateDirectory(oFolder)



'Define the active document as an assembly file
'Dim oAsmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oAsmPN As String = oAsmDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").ItemByPropId(5).Value

'Get user input
Dim RUsure As DialogResult = MessageBox.Show ("This will save a .pdf of all the Ref drawings, and .stp of all related models" _
& vbLf & " " _
& vbLf & "Are you sure you want to proceed with creating these files?" _
& vbLf & "This could take a while!" _
& vbLf & " " _
& vbLf & "We will let you know when this process is finished!", "WCD Drawing Helps", MessageBoxButtons.YesNo,MessageBoxIcon.Question)
If RUsure <> vbYes Then Exit Sub
numFiles = 0





'Look at the files referenced by the assembly and work the referenced models
For Each oRefDoc In oRefDocs 'As Document In oAsmDoc.AllReferencedDocuments 
	'On Error Goto NoProp
	iLogicVb.UpdateWhenDone = True
	Try
	'Catch
	oProp = oRefDoc.PropertySets("Inventor User Defined Properties").Item("Process").Value
	Catch
	End Try
	If oProp Like "*Laser*" Or oProp Like "*Plasma*" Or oProp Like "*Torch*" Or oProp Like "*Cut*" Or oProp Like "*Water*" Or oProp Like "*Weld*" Or oProp Like "*Assem*" Or oProp Like "*Mach*" Or oProp Like "*Purchas*"
	oFolder = ("C:\Inventor PDF\"&"\"& oAModelPN &"\" & oProp &"\")
	If Not System.IO.Directory.Exists(oFolder) Then System.IO.Directory.CreateDirectory(oFolder)

    	If oRefDoc.ComponentDefinition.BOMStructure <> BOMStructureEnum.kNormalBOMStructure Then Continue For
	
    	Dim oModelPN As String
				 oModelPN = oRefDoc.PropertySets("{32853F0F-3444-11D1-9E93-0060B03C1CA6}").ItemByPropId(5).Value
				 
       		oRefDoc.SaveAs(oFolder & "\" & oModelPN & ".stp", True)
			dwgPathName = Left(oRefDoc.FullDocumentName, Len(oRefDoc.FullDocumentName) - 3) & "dwg"
            If System.IO.File.Exists(dwgPathName) Then
			numFiles = numFiles + 1
				Dim oDrawDoc As DrawingDocument
				Dim oSheet As Sheet
                oDrawDoc = ThisApplication.Documents.Open(dwgPathName, True)
                oDrawDoc.Activate
				
							
				
				
				oDataMedium.FileName = oFolder & "\" & oModelPN  & ".pdf"
               
				
				Call PDFAddIn.SaveCopyAs(oDrawDoc, oContext, oOptions, oDataMedium) 
				oDrawDoc.Close(True)
    	'Catch ex As Exception
       		'MessageBox.Show("Error processing " & oCurFileName & vbNewLine & ex.Message, "ilogic")
    	'End Try
		End If
		End If
		Next
NoProp: 
        If Err.Number <> 0 Then
			MessageBox.Show("Process iProp does not exist for: " & vbLf & oRefDoc.FullFileName,"WCD Model Helps",MessageBoxButtons.OK,MessageBoxIcon.Warning)
            'MsgBox("Process iProp does not exist for: " & vbLf & oRefDoc.FullFileName,"WCD Helps")
            Err.Clear
        End If
    

				
				



'Show the folder
If numFiles = 0
MessageBox.Show("There are no drawings with " & oProcess & " to create pdf drawings from!", "WCD Model Helps",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
End If
Exit Sub
MessageBox.Show("Thanks for being efficient!" _
& vbLf & "There are (" & numFiles &") - (" & oProcess & ") files created!", "WCD Model Helps",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
'Open the folder containing the new files
'Shell("explorer.exe " & oFolder,vbNormalFocus) 
End Sub

Sub ConfigurePDFAddinSettings(ByRef PDFAddIn As TranslatorAddIn, ByRef oContext As TranslationContext, ByRef oOptions As NameValueMap, ByRef oDataMedium As DataMedium)
	PDFAddIn = ThisApplication.ApplicationAddIns.ItemById("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
	oContext = ThisApplication.TransientObjects.CreateTranslationContext
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	oOptions = ThisApplication.TransientObjects.CreateNameValueMap
	oOptions.Value("All_Color_AS_Black") = 0
	oOptions.Value("Remove_Line_Weights") = 1
	oOptions.Value("Vector_Resolution") = 2000
	oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
	oOptions.Value("Custom_Begin_Sheet") = 1
	oOptions.Value("Custom_End_Sheet") = 1
	oDataMedium = ThisApplication.TransientObjects.CreateDataMedium
End Sub
0 Likes
676 Views
7 Replies
Replies (7)
Message 2 of 8

DRoam
Mentor
Mentor

After this line:

 

For Each oRefDoc In oRefDocs 'As Document In oAsmDoc.AllReferencedDocuments

you should be able to add this line, and it will skip "Reference" BOM-structure documents:

 

If oRefDoc.ComponentDefinition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then Continue For
0 Likes
Message 3 of 8

darrell.wcd
Enthusiast
Enthusiast

Thanks DRoam

That works fine for the specific Item marked as ref.

But it still opens the parts in the assembly that is marked as ref in the current assembly.

 

I have an assembly with parts and a sub assembly marked as ref, in the top level, the parts in the ref assembly are not set to ref. See picture.... There is a difference between the item set to ref, or set to ref in the current assembly. 

 

When switching to Structured or Parts Only, no parts or assembly show up.

I only want to work with the parts and assemblies and sub assemblies that would show up in Structured or Parts Only.

 

Is there a way to first look through the top items in Model Data and then just keep going from the top down?

 

md.PNG

0 Likes
Message 4 of 8

DRoam
Mentor
Mentor

I see what you mean now. Yes, you want to traverse the assembly's occurrences rather than its referenced documents. Here's some sample code to do that:

 

Sub Main
	'Get assembly
	Dim oAssyDoc As AssemblyDocument = ThisApplication.ActiveDocument 
	
	'Process assembly
	ProcessAssembly(oAssyDoc) 
End Sub

Sub ProcessAssembly(oAssyDoc As AssemblyDocument)
	For Each oOcc As ComponentOccurrence In oAssyDoc.ComponentDefinition.Occurrences
		If oOcc.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then
			'This occurrence has been overridden to reference. Skip it.
			Continue For
		Else If oOcc.Definition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then
			'The document BOM structure is reference. Skip it.
			Continue For
		End If
		
		'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		'~~~~~Your code here with whatever you want to do to the assembly~~~~~
		'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		
		'Process sub-components
		If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then ProcessAssembly(oOcc.Definition.Document) 
	Next 
End Sub
0 Likes
Message 5 of 8

darrell.wcd
Enthusiast
Enthusiast

I just cant seam to get this to work, to open all drawings that are not ref.

I found this bit of code that represents exactly what i want but only to the viewreps.

instead of setting it to visible i want to open the drawing for the part or assembly.

 

I'm pulling my hair out over this....

 

oCompDef = ThisDoc.Document.ComponentDefinition
oAssemblyComponents = oCompDef.Occurrences
oViewRepsCollection = oCompDef.RepresentationsManager.DesignViewRepresentations
oActiveViewRep = oCompDef.RepresentationsManager.ActiveDesignViewRepresentation

Dim oOccurrence As ComponentOccurrence
Dim oViewRep As DesignViewRepresentation

For Each oViewRep In oViewRepsCollection
    oViewRep.Activate 'Activate each view rep one at a time
    For Each oOccurrence In oAssemblyComponents
        If (oOccurrence.BOMStructure = BOMStructureEnum.kReferenceBOMStructure) Then
            oOccurrence.Visible = False
        Else
            oOccurrence.Visible = True
        End If
    Next
Next

oActiveViewRep.Activate 'Sets the view rep back to the one that was active before the rule was executed
0 Likes
Message 6 of 8

DRoam
Mentor
Mentor

@darrell.wcd, I must be unclear on what you're wanting, because the code I posted in my last post should do exactly what you want, when combined with "opener" the code from your original post.

 

From what I understand, you want to open every occurrence in your assembly that is neither reference nor belongs to a reference assembly. Is that correct? If so, the code below should work for you. If it's not correct, please provide a detailed explanation of what you want the code to do, from start to finish.

 

This code should open the drawing for each component in your assembly, unless it's Reference or a child of a Reference assembly. I also added some frills to show feedback at the end of what was opened and what failed to open. It also turns of screen updating while running so every drawing isn't flashing on your screen as it's opened.

 

Sub Main
	'Get assembly
	Dim oAssyDoc As AssemblyDocument = ThisApplication.ActiveDocument 
	
	'Process assembly
	ThisApplication.StatusBarText = "Opening all component drawings..."
	ThisApplication.ScreenUpdating = False
	Try
		ProcessAssembly(oAssyDoc)
	Catch
		MessageBox.Show("An error occurred while trying to process the assembly.","Open All Drawings",MessageBoxButtons.OK,MessageBoxIcon.Error)
		Return
	Finally
		ThisApplication.ScreenUpdating = True
		oAssyDoc.Activate
	End Try
	
	Dim openedString As String = "(" & openedDocs.Count & ") drawings were successfully opened."
	
	If skippedDocs.Count = 0 Then
		MessageBox.Show(openedString,"Open All Drawings",MessageBoxButtons.OK,MessageBoxIcon.Information)
	Else
		MessageBox.Show(openedString & vbCrLf & vbCrLf & "The following components don't have a drawing or their drawing failed to open:" & vbCrLf & vbCrLf & String.Join(vbCrLf,skippedDocs),"Open All Drawings",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
	End If
End Sub

Private openedDocs As New List(Of String)
Private skippedDocs As New List(Of String)

Sub ProcessAssembly(oAssyDoc As AssemblyDocument)
	For Each oOcc As ComponentOccurrence In oAssyDoc.ComponentDefinition.Occurrences
		If oOcc.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then
			'This occurrence has been overridden to reference. Skip it.
			Continue For
		Else If oOcc.Definition.BOMStructure = BOMStructureEnum.kReferenceBOMStructure Then
			'The document BOM structure is reference. Skip it.
			Continue For
		End If
		
		'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		'~~~~~Your code here with whatever you want to do to the occurrence~~~~~
		'~~~~~~~~~In this case, the occurrence's drawing will be opened~~~~~~~~~
		Dim oOccDoc As Document = oOcc.Definition.Document
		Dim oOccDocName As String = oOccDoc.FullFileName
		
		If openedDocs.Contains(oOccDocName) OrElse skippedDocs.Contains(oOccDocName) Then
			' Document already opened or attempted
			Continue For
		Else
			' Open document
			Dim oDrawDoc As DrawingDocument = Nothing
			Try
				Dim dwgPathName As String = Left(oOccDoc.FullFileName, Len(oOccDoc.FullFileName) - 3) & "dwg"
				
				If System.IO.File.Exists(dwgPathName) Then
					Try
						oDrawDoc = ThisApplication.Documents.ItemByName(dwgPathName)
					Catch
					 	oDrawDoc = ThisApplication.Documents.Open(dwgPathName, True)
					End Try
				End If
			Catch
			End Try
			
			If oDrawDoc IsNot Nothing Then
				' Drawing successfully opened or already open.
				openedDocs.Add(oOccDocName)
			Else
				' Drawing file not found or failed to open for some reason.
				skippedDocs.Add(oOccDocName)
			End If
		End If
		'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		
		'Process sub-components
		If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then ProcessAssembly(oOcc.Definition.Document) 
	Next 
End Sub
0 Likes
Message 7 of 8

darrell.wcd
Enthusiast
Enthusiast

@DRoam You got it correct for opening the drawings.
my original code was to open the drawing, create a .pdf of all the drawings and a .stp file of each drawing model, and then close the drawing, is that hard to add that in?

 

I'm pretty much a find, copy, and past iLogic person.... 

slight modifications I can do but this seams to be over my head.

 

Thanks for the reply as this is already helpful.

0 Likes
Message 8 of 8

darrell.wcd
Enthusiast
Enthusiast

@DRoam  that is great, it opens the drawings exactly as needed.

 

I would still like to be able to just export the pdf of the dwg and also the stp of the part in the dwg.

0 Likes