To insert a reference document in to current - Derived Part

To insert a reference document in to current - Derived Part

RoyWickrama_RWEI
Advisor Advisor
1,154 Views
9 Replies
Message 1 of 10

To insert a reference document in to current - Derived Part

RoyWickrama_RWEI
Advisor
Advisor

Master skeleton (example):

"D:\Vault Workspace\Projects\A_RWEI\IEIS 10204\10204-skm0001.ipt"

 

Current document :

"10204-p0001x"

or full file name: "D:\Vault Workspace\Projects\A_RWEI\IEIS 10204\10204-p0001x.ipt"

 

Need the current document be a derived document of the master sketch as a reference document and to select parameters. I can improve to get my results, but I need help o begin with.

 

I request someone help me.

 

Thanks.

 

 

 

 

 

0 Likes
Accepted solutions (1)
1,155 Views
9 Replies
Replies (9)
Message 2 of 10

HideoYamada
Advisor
Advisor

Hello,

 

Which language are you using? (VBA, iLogic, vb.net or c#?)

 

There is the sample code which is written in VBA.

https://forums.autodesk.com/t5/inventor-customization/ilogic-or-vb-code-derived-component/td-p/91310... 

 

=====

Freeradical

 Hideo Yamada

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 3 of 10

RoyWickrama_RWEI
Advisor
Advisor

I need to do with an iLogic rule.

Could you help.

 

Thanks.

0 Likes
Message 4 of 10

HideoYamada
Advisor
Advisor
Accepted solution

Hello,

 

This post comes back from :

https://forums.autodesk.com/t5/inventor-customization/ilogic-or-vb-code-derived-component/m-p/915414...

 

I created iLogic code.

Option Explicit On
Sub Main()
    Dim oDestDoc As PartDocument = ThisApplication.ActiveEditDocument
    Dim oDestDef As PartComponentDefinition = oDestDoc.ComponentDefinition

    ' This is dirty hack
    ' Change the filename from "fullpath\SKIP_DOC0035.ipt" to "fullpath\SKIP_DOC0034.ipt"
    Dim sourceFullFileName As String = oDestDoc.FullFileName
    Mid(sourceFullFileName, Len(sourceFullFileName) - 4, 1) = "4"

    ' Add Derived Part Feature
    Dim oDerivedPartComps As DerivedPartComponents = oDestDef.ReferenceComponents.DerivedPartComponents

    Dim oDerivedDef As DerivedPartUniformScaleDef = oDerivedPartComps.CreateUniformScaleDef(sourceFullFileName)

    ' Exclude all items once.
    oDerivedDef.ExcludeAll

    ' Include the first Solid.
    oDerivedDef.Solids.Item(1).IncludeEntity = True

    ' Include the visible sketches.
    For Each entity As DerivedPartEntity In oDerivedDef.Sketches
        If entity.ReferencedEntity.Visible = True Then
            entity.IncludeEntity = True
        End If
    Next entity

    ' Create the internal dictionary of parameters.
    Dim paramDic As New System.Collections.Generic.Dictionary(Of String, DerivedPartEntity)
    For Each entity As DerivedPartEntity In oDerivedDef.Parameters
        paramDic.Add(entity.ReferencedEntity.Name, entity)
    Next entity

    ' Include Parameters which name is started with "Dim_".
    For Each pair As System.Collections.Generic.KeyValuePair(Of String, DerivedPartEntity) In paramDic
        If pair.Key.StartsWith("Dim_") Then
            pair.Value.IncludeEntity = True
        End If
    Next pair
'    paramDic("Dim_d4").IncludeEntity = True
'    paramDic("Dim_d5").IncludeEntity = True

    ' Include other parameters.
    paramDic("Length").IncludeEntity = True
    paramDic("Width").IncludeEntity = True
    paramDic("Thickness").IncludeEntity = True

    ' Add the definition and create the DerivedPartFeature.
    Dim oDerivedPartComp As DerivedPartComponent = oDerivedPartComps.Add(oDerivedDef)
End Sub

I hope this code helps you.

 

=====

Freeradical

 Hideo Yamada

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
Message 5 of 10

RoyWickrama_RWEI
Advisor
Advisor

If I run the rule for more than one time, it keeps adding the derived part and the parameters as well,

Could you help to get around the issue. Thanks.

2019-11-19 22_27_12-Window.png

 

 

 

0 Likes
Message 6 of 10

HideoYamada
Advisor
Advisor

Hi,

 


@RoyWickrama_RWEI wrote:

If I run the rule for more than one time, it keeps adding the derived part and the parameters as well,

Could you help to get around the issue. Thanks.


What do you think should be done when the rule is invoked multiple times?

Ignore the execution?

 

=====

Freeradical

 Hideo Yamada

 

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 7 of 10

RoyWickrama_RWEI
Advisor
Advisor

Thanks.

I got it around (using your guidance).

 

Option Explicit On
Sub Main
    Dim oThisActiveDoc As PartDocument = ThisApplication.ActiveEditDocument
	Dim oThisActiveDerivedDoc As PartDocument = ThisApplication.ActiveEditDocument						'Use where applicable
    Dim oThisActiveDocDef As PartComponentDefinition = oThisActiveDoc.ComponentDefinition
	Dim oThisActiveDerivedDocDef As PartComponentDefinition = oThisActiveDerivedDoc.ComponentDefinition		'where applicable
	Dim oDP_Exists_YN As String = "NO"

	If oThisActiveDoc.DocumentType <> kPartDocumentObject Then		'ThisApplication.ActiveDocument.DocumentType <> kPartDocumentObject Then
	    MsgBox("Make a Part Document the active document")
		Exit Sub
	End If
	
    Dim sourceFullFileName As String 
	sourceFullFileName = "D:\Vault Workspace\Projects\A_RWEI\IEIS 10204\10204-skm0001.ipt"
	sourceFullFileName = "D:\Vault Workspace\Projects\A_RWEI\SKIP-DOC0034.ipt"
    ' Add Derived Part Feature
	
	oThisActiveDoc = ThisApplication.ActiveDocument
	   ' Add Derived Part Feature
    Dim oDerivedPartComps As DerivedPartComponents = oThisActiveDocDef.ReferenceComponents.DerivedPartComponents
	Dim oDerPartComp_Test As DerivedPartUniformScaleDef = oDerivedPartComps.CreateUniformScaleDef(sourceFullFileName)
		
		
	If oThisActiveDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Count < 1 Then
	    MsgBox("No Derived Part Components in this part")

	Else
		
		'Dim oDoc As Document
		'oDoc = ThisDoc.Document
		Dim oRefFile As FileDescriptor
		Dim oOrigRefName As Object     

		For Each oRefFile In oThisActiveDerivedDoc.File.ReferencedFileDescriptors
		'get the full file path to the original internal ref3erences
		oOrigRefName = oRefFile.FullFileName
		Next oRefFile


		Dim oThisDerPart As DerivedPartComponent	'because this is a derived part
		For Each XX In oThisActiveDerivedDocDef.ReferenceComponents.DerivedPartComponents
			If oOrigRefName = sourceFullFileName Then oDP_Exists_YN = "YES"
'			MessageBox.Show("oOrigRefName: " _
'			& vbLf & oOrigRefName, "Title")
		Next XX
'		For i = 1 To oThisActiveDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Count
'			MessageBox.Show("Message-2: " _
'			& vbLf & oThisActiveDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Item(i).Name, "LOCAL FILE NAME!")
'		Next i
	End If

' Exclude all items once.
    oDerPartComp_Test.ExcludeAll
'    ' Include the first Solid.
'    oDerPartComp_Test.Solids.Item(1).IncludeEntity = True

'    ' Include the visible sketches.
'    For Each entity As DerivedPartEntity In oDerPartComp_Test.Sketches
'        If entity.ReferencedEntity.Visible = True Then
'            entity.IncludeEntity = True
'        End If
'    Next entity

    ' Create the internal dictionary of parameters.
    Dim oParamsDictionary As New System.Collections.Generic.Dictionary(Of String, DerivedPartEntity)
    For Each entity As DerivedPartEntity In oDerPartComp_Test.Parameters
        oParamsDictionary.Add(entity.ReferencedEntity.Name, entity)
    Next entity

    ' Include Parameters which name is started with "Dim_".
    For Each oParam_to_Include As System.Collections.Generic.KeyValuePair(Of String, DerivedPartEntity) In oParamsDictionary
        If oParam_to_Include.Key.StartsWith("Dim_") Then
			oParam_to_Include.Value.IncludeEntity = True
		Else If oParam_to_Include.Key.StartsWith("At_Joint") Then
		    Try
				oParam_to_Include.Value.IncludeEntity = True
			Catch
			End Try
        End If
    Next oParam_to_Include
	
'    oParamsDictionary("Dim_1").IncludeEntity = True
'    oParamsDictionary("Dim_2").IncludeEntity = True

'    ' Include other parameters.
    Try
	oParamsDictionary("HSS_Slot_GapGusset_3d4_PL").IncludeEntity = True
	Catch
	End Try
	
	Try
	oParamsDictionary("HSS_Slot_Rad_Fillet").IncludeEntity = True
	Catch
	End Try

If oDP_Exists_YN = "NO" Then
    ' Add the definition and create the DerivedPartFeature.
	MessageBox.Show("DP ADDED!", "Title")
    Dim oDerivedPartComp As DerivedPartComponent = oDerivedPartComps.Add(oDerPartComp_Test)
End If
Dim oYesNo As String
oYesNo = MessageBox.Show("Do you want to copy custom properties?", "TO COPY CUST. PROPS", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
oSub_Copy_Props(oYesNo)

iLogicVb.UpdateWhenDone = True

End Sub

Sub oSub_Copy_Props(X As String)
	If X = vbYes
		Dim derivedComponent As Inventor.Document = ThisApplication.ActiveDocument
		'if there is only one reference:
		Dim referencedComponent As Inventor.Document = derivedComponent.ReferencedDocuments(1)
		Dim oProps_DerivedComp As PropertySet = derivedComponent.PropertySets.Item("Inventor User Defined Properties")
		Dim oProps_ReferencedComp As PropertySet = referencedComponent.PropertySets.Item("Inventor User Defined Properties")
		Dim oProp As Inventor.Property
		For Each oProp In oProps_ReferencedComp
		Try
		oProps_DerivedComp.Add(oProp.Value, oProp.Name)
		Catch ex As Exception
		oProps_DerivedComp.Add(oProp.Value)
		End Try
		Next
	End If
End Sub
0 Likes
Message 8 of 10

HideoYamada
Advisor
Advisor

Hi,

 

Does your code work properly when the document contains multiple derived part features?

I think there is an issue around "For Each XX" loop.

 

And try consider to use a type "Boolean" instead of "String" if the variable stores only "YES" or "NO".

 

=====

Freeradical

 Hideo Yamada

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes
Message 9 of 10

RoyWickrama_RWEI
Advisor
Advisor

It is fine for the time being.

Today, I had a problem with replacing the derived part reference. The rule I used some times ago, did not do well today. I posted finally for help, just a while ago.

 

 

 

0 Likes
Message 10 of 10

HideoYamada
Advisor
Advisor

Hi,

 


@RoyWickrama_RWEI wrote:

It is fine for the time being.


According to following, I think that the 'sourceFullFileName' will be compared only with a name of the last oRefFile.

The code will work as you expect if the part document has only one DerivedPartFeature.

 

For Each oRefFile In oThisActiveDerivedDoc.File.ReferencedFileDescriptors
    oOrigRefName = oRefFile.FullFileName
Next oRefFile

Dim oThisDerPart As DerivedPartComponent	'because this is a derived part
For Each XX In oThisActiveDerivedDocDef.ReferenceComponents.DerivedPartComponents
    If oOrigRefName = sourceFullFileName Then oDP_Exists_YN = "YES"
Next XX

 

=====

Freeradical

 Hideo Yamada

=====
Freeradical
 Hideo Yamada
https://www.freeradical.jp
0 Likes