Ilogic question, cant pass parameter without reselecting component again.

Ilogic question, cant pass parameter without reselecting component again.

maxim.teleguz
Advocate Advocate
275 Views
2 Replies
Message 1 of 3

Ilogic question, cant pass parameter without reselecting component again.

maxim.teleguz
Advocate
Advocate

I want to be able to save and replace and add a reference from previous component to new one. Right now it works but only if i select new component after the save and replace. Can someone help me code this correctly to allow the code to do this all in one go? 

Thank you!

Imports Inventor
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports System.Windows.Forms
ThisApplication.ScreenUpdating = True
iLogicVb.Automation.RulesOnEventsEnabled = True
ThisApplication.SilentOperation = False



	SendKeys.SendWait("{ESC}")
	AppActivate(ThisApplication.Caption)

	On Error Resume Next
	Dim selectCmd As ControlDefinition
    selectCmd = ThisApplication.CommandManager.ControlDefinitions.Item("AppContextual_CancelCmd")
    selectCmd.Execute

P = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select component to replace")

	Dim odoc As Document = P.Definition.Document
	modelName = IO.Path.GetFileName(odoc.DisplayName)
	
	Dim saveme As String 
	saveme = iProperties.Value(modelName, "Project", "Part Number")
	'MsgBox(saveme)
	ThisApplication.CommandManager.DoSelect(P)
	
	Dim cm As CommandManager = ThisApplication.CommandManager
	Dim cds As ControlDefinitions = cm.ControlDefinitions
	Dim cd As ControlDefinition = cds("AssemblyBonusTools_SaveAndReplaceComponentCmd")
	Call cd.Execute

	M = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select component that you just replaced again to add reference")
			
			Dim odoc1 As Document = M.Definition.Document
			modelName1 = IO.Path.GetFileName(odoc1.DisplayName)
			
			t = "Ref. Number"
			'Try : x = iProperties.Value(modelName1,"Custom", t) : Catch : iProperties.Value(modelName1,"Custom", t) = "" : End Try

			If iProperties.Value(modelName1, "Custom", t) = Nothing Then
				myparam = InputBox("Reference Drawing Number:", t, "REF: " & saveme)
			Else
				myparam = InputBox("Reference Drawing Number:", t, "REF: " & saveme)
			End If

			iProperties.Value(modelName1, "Custom", t) = myparam
			
			'MsgBox(iProperties.Value(modelName1, "Custom", t))
			InventorVb.DocumentUpdate()
0 Likes
Accepted solutions (1)
276 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor

Hi @maxim.teleguz.  I am not that familiar with using the Save & Replace tool, because I do not use it, but I'm thinking you could get that component again by its 'path'.  If all of your component names have been stabilized (renamed differently from default, on purpose) then I'm thinking this should be fairly easy.  But if using save & replace gives the component a different name, then you would have to assume its new name is the new file name.  The 'path' is an Array of ComponentOccurrence objects, where the first item in it is the component that represents the top level sub assembly in the parent hierarchy of the component, then the next component below that one, and so on until the component you are after.  There is also an iLogic shortcut snippet for this purpose called MakePath, where you enter the names of the components in the component's hierarchy as Strings, separated by commas, and it returns an ArrayList type object you can use to specify a component in some situations.  Just some thoughts.

 

I also wander if using Save & Replace on a component changes its Index number within its parent assembly.  If not, you could just get the component's Index before replacing it, then use that same Index to get it again after the save/replace process.  And if it does change its Index, it is most likely going to be the 'Last' one, so you could use the components.Count property to specify it by its Index.  When using the Pick method, and the leaf components filter, if you select a component that is deeper than first level, it will be a ComponentOccurrenceProxy type object.  You can get the 'real' one using its NativeObject property, then you can get its real parent AssemblyComponentDefinition using the real component's Parent property.  Then you can loop its components by their natural Index numbers and use the 'Is' operator to compare if your component Is that component, to get its Index.  Might be more code that you were hoping for though.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 3

maxim.teleguz
Advocate
Advocate
Accepted solution

i just solved it and it was really easy, all it took was some inspiration from you. 

Imports Inventor
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports System.Windows.Forms
ThisApplication.ScreenUpdating = True
iLogicVb.Automation.RulesOnEventsEnabled = True
ThisApplication.SilentOperation = False



	SendKeys.SendWait("{ESC}")
	AppActivate(ThisApplication.Caption)

	On Error Resume Next
	Dim selectCmd As ControlDefinition
    selectCmd = ThisApplication.CommandManager.ControlDefinitions.Item("AppContextual_CancelCmd")
    selectCmd.Execute
	
comps = ThisApplication.TransientObjects.CreateObjectCollection 'new

	P = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select component to replace")
	If IsNothing(P) Then Exit Sub 'new
	comps.Add(P) 'new

	Dim odoc As Document = P.Definition.Document
	modelName = IO.Path.GetFileName(odoc.DisplayName)
	
	Dim saveme As String 
	saveme = iProperties.Value(modelName, "Project", "Part Number")
	'MsgBox(saveme)
	ThisApplication.CommandManager.DoSelect(P)
	
	Dim cm As CommandManager = ThisApplication.CommandManager
	Dim cds As ControlDefinitions = cm.ControlDefinitions
	Dim cd As ControlDefinition = cds("AssemblyBonusTools_SaveAndReplaceComponentCmd")
	Call cd.Execute
	
	M = P 'new
	'M = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Select component that you just replaced again to add reference")
			
			Dim odoc1 As Document = M.Definition.Document
			modelName1 = IO.Path.GetFileName(odoc1.DisplayName)
			
			t = "Ref. Number"
			'Try : x = iProperties.Value(modelName1,"Custom", t) : Catch : iProperties.Value(modelName1,"Custom", t) = "" : End Try

			If iProperties.Value(modelName1, "Custom", t) = Nothing Then
				myparam = InputBox("Reference Drawing Number:", t, "REF: " & saveme)
			Else
				myparam = InputBox("Reference Drawing Number:", t, "REF: " & saveme)
			End If

			iProperties.Value(modelName1, "Custom", t) = myparam
			
			'MsgBox(iProperties.Value(modelName1, "Custom", t))
			InventorVb.DocumentUpdate()