Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
Ralf_Krieg
in reply to: leoa.87

Hello

 

I have sorted your code a little bit and added the DWG export. I'm not able to test it right now.

Please try this and post if errors occur.

Sub Main()

'check that the active document is an assembly file
If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MessageBox.Show("Please run this rule from the assembly file.", "iLogic")
	Exit Sub
End If
'Dim oDoc = ThisApplication.ActiveDocument
Dim oAsmDoc As AssemblyDocument = ThisDoc.Document
Dim sAsmName As String = System.IO.Path.GetFileNameWithoutExtension(oAsmDoc.FullFileName )
Dim sFolder As String = "C:\Users\l.azzariti\Desktop\Output" & "\" & sAsmName & "_" & Parameter("RIF_CMA") & Parameter("RIF_ID") & "Step"


Dim STEPTranslator As TranslatorAddIn
Dim oSTPContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
Dim oSTPOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
Dim oSTPDataMedium As DataMedium  = ThisApplication.TransientObjects.CreateDataMedium
Call ConfigureSTEPTranslatorSettings(STEPTranslator, oSTPContext, oSTPOptions)

Dim DWGTranslator As TranslatorAddIn
Dim oDWGContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
Dim oDWGOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
Dim oDWGDataMedium As DataMedium  = ThisApplication.TransientObjects.CreateDataMedium
Call ConfigureDWGTranslatorSettings(DWGTranslator, oDWGContext)

If Not System.IO.Directory.Exists(sFolder) Then
	System.IO.Directory.CreateDirectory(sFolder)
End If
	

'look at the files referenced by the assembly
Dim oRefDoc As Document
For Each oRefDoc In oAsmDoc.AllReferencedDocuments
	If Not oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then Continue For
	'get all occurrences of the referenced file, if one on them is visible - export that file to STEP, otherwise skip
	Dim oOccs As ComponentOccurrencesEnumerator = oAsmDoc.ComponentDefinition.Occurrences.AllReferencedOccurrences(oRefDoc)
	Dim oOcc As ComponentOccurrence
	Dim bVis As Boolean = False
	For Each oOcc In oOccs
		If oOcc.Visible 
			bVis = True
			Exit For
		End If
	Next
	
	If bVis = False Then
		Continue For 'Next For
	End If
	
	'check that model is saved
	If(System.IO.File.Exists(oRefDoc.FullDocumentName)) Then
		Dim sFilename As String = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullDocumentName)
		Dim sDesc As String = oRefDoc.PropertySets.Item("Design Tracking Properties").Item("Description").Value
		Parameter.Quiet = True
		Dim sRIF_CMA As String = Parameter("RIF_CMA")
		Dim sRIF_ID As String = Parameter("RIF_ID")
		oSTPDataMedium.FileName = sFolder & "\" & sFilename & "_" & sRIF_CMA & "_" & sRIF_ID & sDesc & ".stp"
		oDWGDataMedium.FileName = sFolder & "\" & sFilename & "_" & sRIF_CMA & "_" & sRIF_ID & sDesc & ".dwg"
		
		Try
			Call STEPTranslator.SaveCopyAs(oRefDoc, oSTPContext, oSTPOptions, oSTPDataMedium)
		Catch
			MsgBox("Export of " & oRefDoc.DisplayName & " to STP failed. Continue next one", MsgBoxStyle.Critical)
		End Try
		Try
			Call DWGTranslator.SaveCopyAs(oRefDoc, oDWGContext, oDWGOptions, oDWGDataMedium)
		Catch
			MsgBox("Export of " & oRefDoc.DisplayName & " to DWG failed. Continue next one", MsgBoxStyle.Critical)
		End Try
	End If
Next

MessageBox.Show("New Files Created in: " & vbLf & sFolder, "iLogic")
Shell("explorer.exe " & sFolder,vbNormalFocus)

Dim oAsmCompDef As AssemblyComponentDefinition = oAsmDoc.ComponentDefinition
Dim oRepMgr As RepresentationsManager =oAsmCompDef.RepresentationsManager 
oRepMgr.DesignViewRepresentations.Item("Principale").activate
oRepMgr.LevelofDetailRepresentations("Principale").activate

Dim ExportSheet = sFolder  & "\"  & "Distinta" & "_" & Parameter("RIF_CMA") & "_" & Parameter ("RIF_ID") & ".xls"

Dim oBOM As BOM
oBOM = oAsmDoc.ComponentDefinition.BOM

'' Export the BOM view 
Dim oPartsOnlyBOMView As BOMView
oPartsOnlyBOMView = oBOM.BOMViews.Item("Solo parti")
oPartsOnlyBOMView.Export(ExportSheet, kMicrosoftExcelFormat)

Try
	oRepMgr.DesignViewRepresentations.Item("Vista1").Activate 
Catch
	oRepMgr.DesignViewRepresentations.Add("Vista1")
End Try
Try
	oRepMgr.LevelofDetailRepresentations("Livello di dettaglio1").activate
Catch
	oRepMgr.LevelofDetailRepresentations.Add("Livello di dettaglio1")
End Try
		
End Sub


Sub ConfigureSTEPTranslatorSettings(ByRef oSTEPTranslator As TranslatorAddIn, ByRef oContext As TranslationContext, ByRef oOptions As NameValueMap)
	oSTEPTranslator = ThisApplication.ApplicationAddIns.ItemById("{90AF7F40-0C01-11D5-8E83-0010B541CD80}")

	If oSTEPTranslator.HasSaveCopyAsOptions(ThisApplication.ActiveDocument, oContext, oOptions) Then
		' Set application protocol.
		' 2 = AP 203 - Configuration Controlled Design
		' 3 = AP 214 - Automotive Design
		oOptions.Value("ApplicationProtocolType") = 3
		' Other options...
		'oOptions.Value("Author") = ""
		'oOptions.Value("Authorization") = ""
		'oOptions.Value("Description") = ""
		'oOptions.Value("Organization") = ""
		oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
	End If
End Sub

Sub ConfigureDWGTranslatorSettings(ByRef oDWGTranslator As TranslatorAddIn, ByRef oContext As TranslationContext)
	oDWGTranslator = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")
	oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
End Sub

 


R. Krieg
RKW Solutions
www.rkw-solutions.com