Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

Run a rule for each Metal sheet in assembly and subassembly

Neuzzo
Advocate

Run a rule for each Metal sheet in assembly and subassembly

Neuzzo
Advocate
Advocate

Hi, i've this rule to Publish the documentation for laser cutting and pdf for a metal sheet part.

It run only in a drawing. Is possible to use this in an assembly and subassembly to do this for each metal sheet?

Imports System.Diagnostics
oPath = ThisDoc.Path
'oFileName = ThisDoc.FileName(False) 'without extension
oFileName = Split(ThisDoc.FileName(False), "_T")(0)
oPDFAddIn = ThisApplication.ApplicationAddIns.ItemById _
("{0AC6FD96-2F4D-42CE-8BE0-8AEA580399E4}")
oDocument = ThisApplication.ActiveDocument
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = IOMechanismEnum.kFileBrowseIOMechanism
oOptions = ThisApplication.TransientObjects.CreateNameValueMap
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium 
oDataMedium1 = ThisApplication.TransientObjects.CreateDataMedium 
'oDataMedium2 = ThisApplication.TransientObjects.CreateDataMedium 


If oPDFAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then
oOptions.Value("All_Color_AS_Black") = 0
oOptions.Value("Remove_Line_Weights") = 0
oOptions.Value("Vector_Resolution") = 1200
oOptions.Value("Sheet_Range") = Inventor.PrintRangeEnum.kPrintAllSheets
'oOptions.Value("Custom_Begin_Sheet") = 2'oOptions.Value("Custom_End_Sheet") = 4
End If 

oRefDoc = ThisDrawing.ModelDocument
Dim oPropValue As String 
Dim oPropValue1 As String 

oPropValue = oRefDoc.PropertySets("Summary Information").Item("Revision Number").Value 
iProperties.Value("Summary", "Revision Number") = oPropValue

oPropValue1 = oRefDoc.PropertySets.Item("Design Tracking Properties").Item("Material").Value 
iProperties.Value("Custom", "Material Type") = oPropValue1

If oPropValue1 = "811195 AISI 304 sp. 3 SB + PVC"
                oPropValue1 = "811195 AISI 304 sp. 3.0 SB + PVC"
End If

Rev = iProperties.Value("Summary", "Revision Number")

'get PDF target folder path
Dim oFolder As String 
                oFolder = "\\SRVDOC2016\pubblica\Foto-Articoli-AS400\"

Dim PDFFolder2 As String
                PDFFolder2 = "\\SRVDOC2016\pubblica\Foto-Articoli-AS400_REV_VECCHIE"
                
If Not System.IO.Directory.Exists(oFolder) Then
    System.IO.Directory.CreateDirectory(oFolder)
End If

Dim Materiale As String = iProperties.Value("Custom", "Material Type")

If Not System.IO.Directory.Exists("\\srvdoc2016\PUBBLICA\PRODUZIONE\TAGLIO LASER\" & oPropValue1 & "\") Then
    System.IO.Directory.CreateDirectory("\\srvdoc2016\PUBBLICA\PRODUZIONE\TAGLIO LASER\" & oPropValue1 & "\")
End If

'Set the PDF target file name
oDataMedium.FileName = oFolder & "\" & oFileName & ".pdf" 
oDataMedium1.FileName = PDFFolder2 & "\" & oFileName & "-" & Rev & ".pdf" 

'Publish document
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium) 
oPDFAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium1)

Dim oPart As PartDocument
Dim oPartPath As String

' Get drawing path
oPartPath = Split(ThisDoc.PathAndFileName(False), "_T")(0) & ".ipt"

' Set a reference to the target part
oPart = ThisApplication.Documents.ItemByName(oPartPath)

' Open the target part
ThisApplication.Documents.Open(oPartPath)

InventorVb.DocumentUpdate(True)

Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument

Dim oType As String
oType = oDoc.DocumentSubType.DocumentSubTypeID

Dim currentStyle As String
currentStyle = iProperties.Material

If oType =  "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
      'Set your filepath here:
                'SETFilePath = "\\srvdoc2016\PUBBLICA\PRODUZIONE\TAGLIO LASER"' & currentStyle & "\"

                Dim partDoc As PartDocument

                'Check for flat pattern >> create one if needed
                Dim oDoc01 As PartDocument
                oDoc01 = ThisApplication.ActiveDocument
                Dim oCompDef As SheetMetalComponentDefinition
                oCompDef = oDoc01.ComponentDefinition
                If oCompDef.HasFlatPattern = False Then
                oCompDef.Unfold

                Else
                oCompDef.FlatPattern.Edit
                End If

                'DXF Settings
                Dim sOut As String
                Dim sPATH As String
                sOut = "FLAT PATTERN DWG?AcadVersion=2004&OuterProfileLayer=FUORI&OuterProfileLayerColor=255;0;0&InteriorProfilesLayer=DENTRO&InteriorProfilesLayerColor=255;0;0&BendUpLayer=Layer1&BendUpLayerColor=0;255;255&BendDownLayer=VAPOR&BendDownLayerColor=0;0;255&ToolCenterLayer=Layer3&ToolCenterUpLayer=Layer4&ToolCenterDownLayer=Layer5&FeatureProfilesLayer=Layer6&FeatureProfilesUpLayer=7&FeatureProfilesDownLayer=Layer8&AltRepFrontLayer=Layer9&AltRepBackLayer=Layer9&InvisibleLayers=IV_ARC_CENTERS;IV_TANGENT;IV_BEND;Layer3;Layer4;Layer5;Layer9&BendUpLayerLineType=37644&BendDownLayerLineType=37644&FeatureProfilesDownLayerColor=255;255;0&FeatureProfilesUpLayerColor=255;255;0&FeatureProfilesLayerColor=255;255;0"
                Dim sFname As String
                sFname = "\\srvdoc2016\PUBBLICA\PRODUZIONE\TAGLIO LASER\" & oPropValue1 & "\" & Split(ThisDoc.FileName(False), "_T")(0) & "-" & Rev & ".dwg" ' & "\" & oPropValue1
                pFname = ThisDoc.FileName(False) & ".ipt"
                
                'Export the DXF and fold the model back up
                oCompDef.DataIO.WriteDataToFile( sOut, sFname)
                Dim oSMDef As SheetMetalComponentDefinition
                oSMDef = oDoc01.ComponentDefinition
                oSMDef.FlatPattern.ExitEdit

Else 

      GoTo Update
                  
End If

Update :
InventorVb.DocumentUpdate(False)
opendoc = Split(ThisDoc.PathAndFileName(False), "_T")(0) 'ThisDoc.PathAndFileName()'saves doc path and file name
doc = ThisApplication.Documents.Open(opendoc & ".ipt")'saves 'doc' as the open application from 'opendoc' adding the extension '.iam'
doc.Close 'closes the indicated document 'doc'

InventorVb.DocumentUpdate(False)
InventorVb.DocumentUpdate(False)

Ive try this but no luck for me

iLogicVb.UpdateWhenDone = True
Dim oAsm As AssemblyDocument = ThisDoc.Document
For Each oRefDoc As Document In oAsm.AllReferencedDocuments
	If oRefDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject AndAlso _
		oAsm.ComponentDefinition.Occurrences.AllReferencedOccurrences(oRefDoc).Count > 0
		On Error Resume Next
		iLogicVb.Automation.RunRule(oRefDoc, "iLogic_rule")
	End If
Next

What i want to do is:

In assemlby file, for each 

{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}

 

in assembly open Drawing, run rule, close drawing, next.

 

Thank you all

Danilo "DannyGi" G.
Mechanical design engineer and product developer
0 Likes
Reply
Accepted solutions (1)
452 Views
6 Replies
Replies (6)

WCrihfield
Mentor
Mentor

Hi @Neuzzo.  The following iLogic rule might work to satisfy your request near the end of you post, for iterating through an assembly, getting drawing documents of referenced model files, then running your existing rule on those drawings.  However, you will have to change a few things in your current iLogic rule to make this scenario work.  In your existing rule, anywhere you are using the term 'ThisApplication.ActiveDocument', you will need to replace that with 'ThisDoc.Document', because the other term will always get the main assembly.  Also, your lines of code using the iLogic shortcut snippet 'iProperties.Value()' will most likely have to change also, because they will most likely not be targeting the right document anymore in this situation.  Doing it with API code would likely be your safest bet.

 

Dim oADoc As AssemblyDocument = ThisDoc.Document
For Each oRefDoc As Document In oADoc.AllReferencedDocuments
	If oRefDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For
	'try to find this referenced document's drawing file
	'assumes same path & name as model, but with ".idw" extension
	Dim oDDocFile As String = System.IO.Path.ChangeExtension(oRefDoc.FullFileName, ".idw")
	If System.IO.File.Exists(oDDocFile) Then
		Dim oDDoc As DrawingDocument = ThisApplication.Documents.Open(oDDocFile, False)
		i = iLogicVb.Automation.RunRule(oDDoc, "iLogic_rule")
		'if i = 0, it ran OK, else errors happened
		oDDoc.ReleaseReference
	End If
Next
ThisApplication.Documents.CloseAll(True) 'close all unreferenced documents (clean-up)

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Neuzzo
Advocate
Advocate

Hi, i've try your macro but it doen't open nothing, Apparently nothing to do.
Ps: our drawing have at the end of nome the suffix "_T"
ex: 61000100.ipt
61000100_T.idw
My external rule is called " Pubblica_Documenta"

Dim oADoc As AssemblyDocument = ThisDoc.Document
For Each oRefDoc As Document In oADoc.AllReferencedDocuments
	If oRefDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For
	'try to find this referenced document's drawing file
	'assumes same path & name as model, but with ".idw" extension
	Dim oDDocFile As String = System.IO.Path.ChangeExtension(oRefDoc.FullFileName,"_T.idw")
	If System.IO.File.Exists(oDDocFile) Then
		Dim oDDoc As DrawingDocument = ThisApplication.Documents.Open(oDDocFile, False)
		i = iLogicVb.Automation.RunRule(oDDoc, "Pubblica_Documenta")
		'if i = 0, it ran OK, else errors happened
		oDDoc.ReleaseReference
	End If
Next
ThisApplication.Documents.CloseAll(True) 'close all unreferenced documents (clean-up)
Danilo "DannyGi" G.
Mechanical design engineer and product developer
0 Likes

WCrihfield
Mentor
Mentor

OK.  I did not realize about the "_T" at the ends of drawing file names, and did not realize the rule was external.  I changed the code appropriately to compensate for those two things.  Now, I am getting the path & name of the file, without its extension, then putting those pieces together with the "_T.idw" at the end of the drawing file's name.  I don't think trying to add that would have worked right while using the ChangeExtension method.  The code is now using the RunExternalRule method, instead of the RunRule method, since it is an external rule.  Also, I currently have the drawings set to open invisibly, instead of visibly.  If you would rather see them visibly open, you can change False to True at the end of the Documents.Open() method line.  See if this works any better for you.

Dim oADoc As AssemblyDocument = ThisDoc.Document
If oADoc.AllReferencedDocuments.Count = 0 Then Exit Sub
For Each oRefDoc As Document In oADoc.AllReferencedDocuments
	'if it is not a Sheet Metal Part, skip to next oRefDoc
	If oRefDoc.SubType <> "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For
	'try to find this referenced document's drawing file
	Dim oPath As String = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName)
	Dim oName As String = System.IO.Path.GetFileNameWithoutExtension(oRefDoc.FullFileName)
	Dim oDDocFile As String = oPath & "\" & oName & "_T.idw"
	If System.IO.File.Exists(oDDocFile) Then
		Dim oDDoc As DrawingDocument = ThisApplication.Documents.Open(oDDocFile, False)
		i = iLogicVb.Automation.RunExternalRule(oDDoc, "Pubblica_Documenta")
		'if i = 0, it ran OK, else errors happened
		oDDoc = Nothing
		oDDoc.ReleaseReference
	End If
Next
ThisApplication.Documents.CloseAll(True) 'close all unreferenced documents (clean-up)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Neuzzo
Advocate
Advocate

It's work, opena the first part, make dwg but after that , i've this error

Neuzzo_0-1669124950508.png

on 

 

		oDDoc.ReleaseReference

 

And after first part, it stop and not continue with the other parts

 

Danilo "DannyGi" G.
Mechanical design engineer and product developer
0 Likes

WCrihfield
Mentor
Mentor
Accepted solution

I can't read what the error dialog says, because it is in a language I am not fluent in.  That line of code 'oDDoc.ReleaseReference', is mainly used in situations where you have opened a document invisibly, in the background, have maybe read data from it, and are now done with it.  It flags that document to be truly closed, and flushed from memory, the next time you use the Documents.CloseAll(True) method, to help with performance.  The alternative would be to use something like 'oDDoc.Close()', but that is primarily used to close visibly opened documents.  Neither one of those will work to truly close a document, if the document is still being referenced by an open drawing or assembly.  But since the drawing is not actually referenced by the assembly, we should be able to close it after we have ran the rule on it.  There is no way of telling if the rule has fully finished working with the drawing yet though, before it may be attempting to either release the reference to that drawing, or close it.  You could just comment out that line, or delete it, and not attempt to close the drawings.  But that would mean potentially having a bunch of drawings still open after all the codes have finished.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

Neuzzo
Advocate
Advocate

Ok, i've comment the line and tryied and it works! Thank you!

Danilo "DannyGi" G.
Mechanical design engineer and product developer
0 Likes