Component.IsActive inside for each loop

Component.IsActive inside for each loop

sigurdurie
Participant Participant
916 Views
12 Replies
Message 1 of 13

Component.IsActive inside for each loop

sigurdurie
Participant
Participant

Hi

 

I'm trying to check wether a component is active i.e. not suppressed in a for each loop to get the DXF if and only if the part is active in the assembly.

I've tried just about anything but without any success.

 

The rule is as follows:

 

question = MessageBox.Show("Do you want to save to DXF?", "iLogic Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
'set condition based on answer
If question = vbYes Then
	' ==============================* Get DXF *====================================
	DXF_LOC = ThisDoc.Path & "\DXF\"
	Dim asmDoc As AssemblyDocument = ThisApplication.ActiveDocument 
	Dim oDoc As Inventor.Document
	For Each oDoc In asmDoc.AllReferencedDocuments
		If Not oDoc.DocumentSubType.DocumentSubTypeID = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For
		 oDocName = oDoc.Displayname
		' If component is Active proceed
			If iProperties.Value(oDocName ,"Summary", "Keywords") = "ISM" Then
				Dim oCompDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition
				' Check if part has flat pattern, create if not
				If Not oCompDef.HasFlatPattern Then
					oCompDef.Unfold
					oDoc.Save
					oDoc.Close
				End If
				' DXF config
				Dim sOut As String = "FLAT PATTERN DXF?" _
				+ "AcadVersion=2004" _
				+ "&OuterProfileLayer=IV_OUTER_PROFILE:Visibility=ON;LinePattern=28100;LineWeight=0,0500;Color=0,128,0;" _
				+ "&InvisibleLayers=IV_TANGENT;IV_BEND;IV_BEND_DOWN;IV_ROLL_TANGENT;IV_ARC_CENTERS"
				
				Dim sFname As String = ""
				
				Select Case oDocName
					Case "HB132.ipt":          	sFname = "IF-H-LAMIR-" & FVG & "-" & Parameter("HB132:1", "Lengd") & "X" & Parameter("HB132:1", "Breidd")
					Case "HB133.ipt":          	sFname = "IF-H-LASINN-" & FVG & "-" & Parameter("HB133:1", "Lengd") & "X" & Parameter("HB133:1", "Breidd")
					Case "IFK-YFIRSTYKKI.ipt": 	sFname = "IFK-YFIRSTYKKI-" & Parameter("IFK-YFIRSTYKKI:1", "Lengd") & "X" & Parameter("IFK-YFIRSTYKKI:1", "Breidd")
					Case "PANNA-H.ipt":        	sFname = "PANNA-H-" & Parameter("PANNA-H:1", "Lengd") & "X" & Parameter("PANNA-H:1", "Breidd")
					Case "BYRÐI-H.ipt":        	sFname = "BYRDI-" & BNFP & "-H-" & Parameter("BYRÐI-H:1", "Lengd") & "X" & Parameter("BYRÐI-H:1", "Breidd")
					Case "Skuffa-LL.ipt":           sFname = "SKUFFA-LL-" & Parameter("Skuffa-LL:1", "Lengd") & "X" & Parameter("Skuffa-LL:1", "Breidd")
					Case "Thr-Profill.ipt":		sFname = "Thr-Profill-" & Parameter("Thr-Profill:1", "Lengd") & "X" & Parameter("Thr-Profill:1", "Breidd")
					Case Else:                 ' Halda áfram
				End Select
	
				If sFname <> vbNullString Then
					oCompDef.DataIO.WriteDataToFile(sOut, DXF_LOC & sFname & ".dxf")
				End If
				
			End If
		' End If
	Next
	'Component.IsActive("Skuffa-LL:1") = 0
	RuleParametersOutput()
	InventorVb.DocumentUpdate()
Else
	' Nothing
End If

I have no idea how or what to use to get the IsActive to work. I've been trying to find answers here and elsewere and tried to adjust what I have found to my specific problem without success.

 

How do I go from a document to an occurrence in the assembly: HB132.ipt -> HB132:1

 

Is there a simple way to do this?

 

 

 

0 Likes
Accepted solutions (2)
917 Views
12 Replies
Replies (12)
Message 2 of 13

Owner2229
Advisor
Advisor
Accepted solution

Hi, the code you're currently ussing is going through every document reference (one per open assembly and subassemblies) instead of component occurrence (many times per assembly) in order to make it run faster. In case your assemblies have many occurrences per part, the current code would run faster. However if you have many subassemblies suppresed and in these are parts that doesn't occurre in other assemblies, the occurrence-way code might run faster. In the end, you can test both and see. Here is the occurrence-way:

Red-highlighted is what I've changed.

 

Sub Main()
question = MessageBox.Show("Do you want to save to DXF?", "iLogic Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question) ' Set condition based on answer If Not question = vbYes Then Exit Sub

DXF_LOC = ThisDoc.Path & "\DXF\"
Dim asmDoc As AssemblyDocument = ThisApplication.ActiveDocument
GoThroughOccs(asmDoc)
RuleParametersOutput()

InventorVb.DocumentUpdate()
End Sub

Private DXF_LOC As String

Sub GoThroughOccs(asmDoc As AssemblyDocument) ' ==============================* Get DXF *==================================== Dim oOcc As ComponentOccurrence For Each oOcc In asmDoc.ComponentDefinition.Occurrences
If Not oOcc.IsActive Then Continue For

Dim oDoc As Inventor.Document = oOcc.Definition.Document
If oDoc.DocumentType.ToString = "kAssemblyDocumentObject" Then GoThroughOccs(oDoc) If Not oDoc.DocumentSubType.DocumentSubTypeID = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For Dim oDocName As String = oDoc.Displayname If iProperties.Value(oDocName ,"Summary", "Keywords") <> "ISM" Then Continue For Dim oCompDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition ' Check if part has flat pattern, create if not If Not oCompDef.HasFlatPattern Then oCompDef.Unfold oDoc.Save oDoc.Close End If ' DXF config Dim sOut As String = "FLAT PATTERN DXF?" _ + "AcadVersion=2004" _ + "&OuterProfileLayer=IV_OUTER_PROFILE:Visibility=ON;LinePattern=28100;LineWeight=0,0500;Color=0,128,0;" _ + "&InvisibleLayers=IV_TANGENT;IV_BEND;IV_BEND_DOWN;IV_ROLL_TANGENT;IV_ARC_CENTERS" Dim sFname As String = "" Select Case oDocName Case "HB132.ipt": sFname = "IF-H-LAMIR-" & FVG & "-" & Parameter("HB132:1", "Lengd") & "X" & Parameter("HB132:1", "Breidd") Case "HB133.ipt": sFname = "IF-H-LASINN-" & FVG & "-" & Parameter("HB133:1", "Lengd") & "X" & Parameter("HB133:1", "Breidd") Case "IFK-YFIRSTYKKI.ipt": sFname = "IFK-YFIRSTYKKI-" & Parameter("IFK-YFIRSTYKKI:1", "Lengd") & "X" & Parameter("IFK-YFIRSTYKKI:1", "Breidd") Case "PANNA-H.ipt": sFname = "PANNA-H-" & Parameter("PANNA-H:1", "Lengd") & "X" & Parameter("PANNA-H:1", "Breidd") Case "BYRÐI-H.ipt": sFname = "BYRDI-" & BNFP & "-H-" & Parameter("BYRÐI-H:1", "Lengd") & "X" & Parameter("BYRÐI-H:1", "Breidd") Case "Skuffa-LL.ipt": sFname = "SKUFFA-LL-" & Parameter("Skuffa-LL:1", "Lengd") & "X" & Parameter("Skuffa-LL:1", "Breidd") Case "Thr-Profill.ipt": sFname = "Thr-Profill-" & Parameter("Thr-Profill:1", "Lengd") & "X" & Parameter("Thr-Profill:1", "Breidd") Case Else: ' Halda áfram End Select If sFname <> vbNullString Then oCompDef.DataIO.WriteDataToFile(sOut, DXF_LOC & sFname & ".dxf") End If Next End Sub

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 13

sigurdurie
Participant
Participant

Thank you for the response

 

I'm getting an error probably because I had to change it a little. I put everything before the save to DXF to sub Main() there can be only one sub main like this:

 

Sub Main()
   ' Configure and build the door and then ask if user wants to save to DXF
' (About 200 lines of code for this type of door, more doors to come) question = MessageBox.Show("Do you want to save to DXF?", "iLogic Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question) ' Set condition based on answer If question = vbYes Then SaveToDXF() Else ' Nothing End If End Sub
Sub SaveToDXF()
    question = MessageBox.Show("Do you want to save to DXF?", "iLogic Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
    ' Set condition based on answer
    If Not question = vbYes Then Exit Sub

    DXF_LOC = ThisDoc.Path & "\DXF\"
    Dim asmDoc As AssemblyDocument = ThisApplication.ActiveDocument
    GoThroughOccs(asmDoc)
    RuleParametersOutput()
    InventorVb.DocumentUpdate()
End Sub

Private DXF_LOC As String

Sub GoThroughOccs(asmDoc As AssemblyDocument)
    ' ==============================* Get DXF *====================================
    Dim oOcc As ComponentOccurrence
    For Each oOcc In asmDoc.ComponentDefinition.Occurrences
        If Not oOcc.IsActive Then Continue For
        Dim oDoc As Inventor.Document = oOcc.Definition.Document
        If oDoc.DocumentType.ToString = "kAssemblyDocumentObject" Then GoThroughOccs(oDoc)
            If Not oDoc.DocumentSubType.DocumentSubTypeID = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For
            Dim oDocName As String = oDoc.Displayname
            If iProperties.Value(oDocName ,"Summary", "Keywords") <> "ISM" Then Continue For
                Dim oCompDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition
                ' Check if part has flat pattern, create if not
                If Not oCompDef.HasFlatPattern Then
                    oCompDef.Unfold
                    oDoc.Save
                    oDoc.Close
        End If
        ' DXF config
        Dim sOut As String = "FLAT PATTERN DXF?" _
        + "AcadVersion=2004" _
        + "&OuterProfileLayer=IV_OUTER_PROFILE:Visibility=ON;LinePattern=28100;LineWeight=0,0500;Color=0,128,0;" _
        + "&InvisibleLayers=IV_TANGENT;IV_BEND;IV_BEND_DOWN;IV_ROLL_TANGENT;IV_ARC_CENTERS"
        
        Dim sFname As String = ""
            
        Select Case oDocName
            Case "HB132.ipt":              sFname = "IF-H-LAMIR-" & FVG & "-" & Parameter("HB132:1", "Lengd") & "X" & Parameter("HB132:1", "Breidd")
            Case "HB133.ipt":              sFname = "IF-H-LASINN-" & FVG & "-" & Parameter("HB133:1", "Lengd") & "X" & Parameter("HB133:1", "Breidd")
            Case "IFK-YFIRSTYKKI.ipt":     sFname = "IFK-YFIRSTYKKI-" & Parameter("IFK-YFIRSTYKKI:1", "Lengd") & "X" & Parameter("IFK-YFIRSTYKKI:1", "Breidd")
            Case "PANNA-H.ipt":            sFname = "PANNA-H-" & Parameter("PANNA-H:1", "Lengd") & "X" & Parameter("PANNA-H:1", "Breidd")
            Case "BYRDI-H.ipt":            sFname = "BYRDI-" & BNFP & "-H-" & Parameter("BYRDI-H:1", "Lengd") & "X" & Parameter("BYR�I-H:1", "Breidd")
            Case "Skuffa-LL.ipt":          sFname = "SKUFFA-LL-" & Parameter("Skuffa-LL:1", "Lengd") & "X" & Parameter("Skuffa-LL:1", "Breidd")
            Case "Thr-Profill.ipt":        sFname = "Thr-Profill-" & Parameter("Thr-Profill:1", "Lengd") & "X" & Parameter("Thr-Profill:1", "Breidd")
            Case Else:                     ' Continue (more cases to come)
        End Select
    
        If sFname <> vbNullString Then
            oCompDef.DataIO.WriteDataToFile(sOut, DXF_LOC & sFname & ".dxf")
        End If
    Next
End Sub

But this returns an error for the IsActive:

 

ErrorInRule01.png

More info:

ErrorInRule02.png

 

Something about public and private members. I tried to omit the sub main() and other call to function but got a similar error on the IsActive.

 

 

 

0 Likes
Message 4 of 13

sigurdurie
Participant
Participant

By changing the line:

 

If Not oOcc.IsActive Then Continue For

 

To:

 

If Not Component.IsActive(oOcc.Name) Then Continue For

I stop getting the error but no DXF is returned to the DXF folder.

 

 

 

0 Likes
Message 5 of 13

Owner2229
Advisor
Advisor
Accepted solution

Hi, sorry, I've overseen this one. Try this instead:

 

If oOcc.Suppresed Then Continue For

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 6 of 13

sigurdurie
Participant
Participant

Same result -> No error but nothing turnes up in the DXF folder

 

I have confirmed that the path is ok by printing out the DXF_LOC so at least that is working. But nothing gets printed after the if statement no matter how it is formed.

0 Likes
Message 7 of 13

Owner2229
Advisor
Advisor

Alright, Let's try it once again:

 

Btw.: I've tested it and it works for me.

 

Sub Main()
    question = MessageBox.Show("Do you want to save to DXF?", "iLogic Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
    ' Set condition based on answer
    If Not question = vbYes Then Exit Sub

    DXF_LOC = ThisDoc.Path & "\DXF\"
    Dim asmDoc As AssemblyDocument = ThisApplication.ActiveDocument
    GoThroughOccs(asmDoc)
    RuleParametersOutput()
    InventorVb.DocumentUpdate()
End Sub

Private DXF_LOC As String

Sub GoThroughOccs(asmDoc As AssemblyDocument)
	' ==============================* Get DXF *====================================
	Dim oOcc As ComponentOccurrence
	For Each oOcc In asmDoc.ComponentDefinition.Occurrences
                If oOcc.Suppressed Then Continue For
                Dim oDoc As Inventor.Document = oOcc.Definition.Document
                If oDoc.DocumentType.ToString = "kAssemblyDocumentObject" Then GoThroughOccs(oDoc)
		If Not oDoc.DocumentSubType.DocumentSubTypeID = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then Continue For
		Dim oDocName As String = oDoc.Displayname
		If iProperties.Value(oDocName ,"Summary", "Keywords") <> "ISM" Then Continue For
		Dim oCompDef As SheetMetalComponentDefinition = oDoc.ComponentDefinition
		' Check if part has flat pattern, create if not
		If Not oCompDef.HasFlatPattern Then
			oCompDef.Unfold
oCompDef.FlatPattern.ExitEdit oDoc.Save oDoc.Close End If ' DXF config Dim sOut As String = "FLAT PATTERN DXF?" _ + "AcadVersion=2004" _ + "&OuterProfileLayer=IV_OUTER_PROFILE:Visibility=ON;LinePattern=28100;LineWeight=0,0500;Color=0,128,0;" _ + "&InvisibleLayers=IV_TANGENT;IV_BEND;IV_BEND_DOWN;IV_ROLL_TANGENT;IV_ARC_CENTERS" Dim sFname As String = "" Select Case oDocName Case "HB132.ipt": sFname = "IF-H-LAMIR-" & FVG & "-" & Parameter("HB132:1", "Lengd") & "X" & Parameter("HB132:1", "Breidd") Case "HB133.ipt": sFname = "IF-H-LASINN-" & FVG & "-" & Parameter("HB133:1", "Lengd") & "X" & Parameter("HB133:1", "Breidd") Case "IFK-YFIRSTYKKI.ipt": sFname = "IFK-YFIRSTYKKI-" & Parameter("IFK-YFIRSTYKKI:1", "Lengd") & "X" & Parameter("IFK-YFIRSTYKKI:1", "Breidd") Case "PANNA-H.ipt": sFname = "PANNA-H-" & Parameter("PANNA-H:1", "Lengd") & "X" & Parameter("PANNA-H:1", "Breidd") Case "BYRÐI-H.ipt": sFname = "BYRDI-" & BNFP & "-H-" & Parameter("BYRÐI-H:1", "Lengd") & "X" & Parameter("BYRÐI-H:1", "Breidd") Case "Skuffa-LL.ipt": sFname = "SKUFFA-LL-" & Parameter("Skuffa-LL:1", "Lengd") & "X" & Parameter("Skuffa-LL:1", "Breidd") Case "Thr-Profill.ipt": sFname = "Thr-Profill-" & Parameter("Thr-Profill:1", "Lengd") & "X" & Parameter("Thr-Profill:1", "Breidd") Case Else: sFname = Left(oDocName, Len(oDocName) - 4) End Select If sFname <> vbNullString Then oCompDef.DataIO.WriteDataToFile(sOut, DXF_LOC & sFname & ".dxf") End If Next End Sub

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 8 of 13

sigurdurie
Participant
Participant

Ok, some progress I actually get further than I initially thought.

 

I get all the way to this line:

 

SyntaxEditor Code Snippet

If iProperties.Value(oDocName ,"Summary", "Keywords") <> "ISM" Then Continue For

after that nothing gets printed.

 

0 Likes
Message 9 of 13

sigurdurie
Participant
Participant

sFname is always a nullstring, I think that is because oDoc does not look at sub assemblies?

0 Likes
Message 10 of 13

Owner2229
Advisor
Advisor

Well, yea...

 

I have no idea why do you have it there in the first place...

It is looking for a value "ISM" in the iProperty "Keywords" (should be "Kulcsszavak" in Hungarian) and skips this document if it can't find it.

If you have put it there unintentionaly, then you can remove it or comment it out.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 11 of 13

sigurdurie
Participant
Participant

 

This line is actually to ensure I only get the parts that I need for the punching machine, everything else does not matter.

 

ISM -> Is Sheet Metal

 

Could be anything: PM for punching machine for instance

 

It's just a way to try to get only these parts to the DXF folder

0 Likes
Message 12 of 13

sigurdurie
Participant
Participant

It's there intentionally, it's a way to ensure I only get the parts I need to the DXF folder.

 

I could have used PM (Punching Machine) for instance

 

I only need these parts saved as DXF other sheet metal parts are not needed for the punching machine on a day to day bases. They are batch produced and are always the same. 

0 Likes
Message 13 of 13

sigurdurie
Participant
Participant

Solved 🙂

 

With a little tweaking I managed to get exactly what I was looking for with your help.

 

Thank you wery much for your time and effort it's highly appreciated.

 

Ended with omitting the iProperties out and used this instead:

 

Select Case oOcc.Name
			Case "HB132:1":
0 Likes