Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Rename & Replace Component

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
job_delacruz
1964 Views, 6 Replies

Rename & Replace Component

Hello Everyone,

 

I have a little understanding in Ilogic but manage to do some basic customization.

My objective in the Pipe Spool assembly is that anytime the pipe size change both the flanges and pipe part will resize within the assembly. I already resolve this using creating Ilogic Rule and the new parts are creating at the background

My next objective is the Pipe and Flanges will be automatically replaced by the new size (newly created part). This is the area I'm having a problem. Anyone could help me with this one?

 

The Rule that I created is base in this link:

http://beinginventive.typepad.com/being-inventive/2010/04/replacing-and-renaming-components-in-an-as....

 

Assembly Rule:

fmgr = ThisApplication.Filemanager
For Each file In fmgr.files
doc = ThisApplication.Documents.ItemByName(file.fullfileName)
compdef = doc.ComponentDefinition
 
If (doc.documenttype = 12291) Then ' only do this for assembly or subassembly For Each occ In compdef.Occurrences
    If (occ.definition.type =83886592) Then ' only do this for part  Parameter.Quiet = True
    If  Parameter(occ.Name, "length") AndParameter(occ.Name, "length") And Parameter(occ.Name,"length") Then
  Parameter(occ.Name, "length") = Ceil( 10*(occ.definition.RangeBox.MaxPoint.X -occ.definition.RangeBox.MinPoint.X))
  Parameter(occ.Name, "width") = Ceil( 10*(occ.definition.RangeBox.MaxPoint.Y -occ.definition.RangeBox.MinPoint.Y))
  Parameter(occ.Name, "height") = Ceil( 10*(occ.definition.RangeBox.MaxPoint.Z -occ.definition.RangeBox.MinPoint.Z))
      iLogicVb.RunRule(occ.Name, "Part number")
   Component.Replace(occ.Name,iProperties.Value(occ.Name,"Project", "Part Number") &".ipt", False)
      occ.Name =  iProperties.Value(occ.Name,"Project","Part Number")
        End If
    End If
 Next occ
End If
Next file
InventorVb.DocumentUpdate()

 

From there I customize the Rule:

(Need help to include a code for "Automatic Replace Component")

fmgr = ThisApplication.Filemanager 
For Each file In fmgr.files
doc = ThisApplication.Documents.ItemByName (file.fullfileName)
compdef = doc.ComponentDefinition

If (doc.documenttype = 12291) Then ' only do this for assembly or subassembly For Each occ In compdef.Occurrences
    If (occ.definition.type =83886592) Then ' only do this for part     Parameter.Quiet = True
            Parameter(occ.Name, "Size") = Size
            Parameter(occ.Name, "LG")    = LG
              iLogicVb.RunRule(occ.Name, "Save As")
    End If
 Next occ
End If
Next file


Please see also attached file of the assembly I am working on. Thanks!!!

 

Tags (1)
6 REPLIES 6
Message 2 of 7
job_delacruz
in reply to: job_delacruz

I added the  "Component.Replace" and  now the parts are being replace by the new defined size.

However, what I noticed is the Component Occurence Name does not change accorrding to size.

What code I need to add to resolve this issue? 

 

fmgr = ThisApplication.Filemanager 
For Each file In fmgr.files
doc = ThisApplication.Documents.ItemByName (file.fullfileName)
compdef = doc.ComponentDefinition
Dim occ As ComponentOccurrence


If (doc.documenttype = 12291) Then ' only do this for assembly or subassembly For Each occ In compdef.Occurrences
    If (occ.definition.type =83886592) Then ' only do this for part     Parameter.Quiet = True
          Parameter(occ.Name, "Size") = Size1
          Parameter(occ.Name, "LG") = LG1
          iLogicVb.RunRule(occ.Name, "Save As")
          Component.Replace(occ.Name,iProperties.Value(occ.Name,"Project", "Part Number") &".ipt", False) ' New added line to replace the component
 Next occ
End If
Next file

 

 

Message 3 of 7
jdkriek
in reply to: job_delacruz

Try this, it will reset the Occurrence name back to it's default - which will update it to show the new size.

 

If ThisDoc.Document.DocumentType = 12291 Then 'If Assy Doc
	Dim oAssy As AssemblyDocument = ThisApplication.ActiveDocument
	Dim thisOcc As Integer
		'Go through all Occurrences
		For thisOcc = 1 To oAssy.ComponentDefinition.Occurrences.count
			'Reset name to default
			oAssy.ComponentDefinition.Occurrences(thisOcc).Name = ""
		Next thisOcc
End If
Jonathan D. Kriek
MFG Solutions Engineer
KETIV Technologies, Inc.


Message 4 of 7
job_delacruz
in reply to: jdkriek

Thanks! I'll try this code right now. 

Message 5 of 7
jdkriek
in reply to: job_delacruz

You're welcome 😉

Jonathan D. Kriek
MFG Solutions Engineer
KETIV Technologies, Inc.


Message 6 of 7
MaheshwarMD
in reply to: jdkriek

Hello,

 

I would like to run a ilogic rule in any assembly environment so that it has to allow me to rename components in series say "A01, A02....or PJ12, PJ13  or in any series ".

I had tried the code, please refer to the attachment, this code worked well, it had renamed all the components in assembly environment.

But when I open the part file in a separate window, the component name is reverted back to old one before applying iLogic code.

Please let me know how to fix this.

 

Regards,

Mahesh

Mahesh
Message 7 of 7

Thanks a lot for the code snippet in iLogic. I used this snippet and wrote the following VB .NET program to replace and rename component in assembly

 

Sub CompReplace(oldPart As String, newPart As String, newCompName As String)
   Try
        If (Not (System.IO.File.Exists(newPart))) Then
            MessageBox.Show("CompReplace -- New replacement part File >" + newPart + "< does not exist")
            Return
        End If

        ' Top-level assembly document and assembly component definition
        Dim oDoc As AssemblyDocument
        oDoc = InventorApplication.ActiveDocument
        Dim oAsmDef As AssemblyComponentDefinition = oDoc.ComponentDefinition

 

        'Iterate through all of the occurrences at the top level
        For Each oOcc As ComponentOccurrence In oAsmDef.Occurrences
             'get name of this occurrence
             Dim compName As String = oOcc.Name
             MessageBox.Show("CompReplace --- Comp Occurence Name >" + compName + "<")
             If (UCase(compName) = UCase(oldPart)) Then
                   MessageBox.Show("CompReplace -- Component Match found")
                   ' Replace the identified part with the replacement part
                   oOcc.Replace(newPart, False)
                   oOcc.Name = newCompName
                   MessageBox.Show("CompReplace -- Component Replaced AND Renamed")
                   Exit For
            End If
       Next
   Catch ex As Exception
          MessageBox.Show(ex.Message)
          MessageBox.Show(ex.StackTrace)
    End Try
End Sub

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report