I need help with makros (Rules or VBA) in an assembly

I need help with makros (Rules or VBA) in an assembly

to_schroeder
Contributor Contributor
1,113 Views
8 Replies
Message 1 of 9

I need help with makros (Rules or VBA) in an assembly

to_schroeder
Contributor
Contributor

Sadly i know nothing about inventor VBA but i have my fair bit of experience in excel vba. i do not have any experience with rules. So i try to nicely ask this community to help me achieve my goal.

 

Basicly what i want is a rule or VBA Makro that makes it so that if i run it following things happen in this order:

 

  1. Isolate selected parts
  2. Create a representation view
  3. If more than 1 part is selected open a Prompt and ask for the name of the view; else name the view as currently selected part
  4. Lock the created representation view
  5. (If needed) go back to the view in which you ran the rule/makro

 

I currently work with inventor 2021 on win 10.

0 Likes
Accepted solutions (1)
1,114 Views
8 Replies
Replies (8)
Message 2 of 9

garrygoldberg1
Contributor
Contributor

Sub Main() 'Declare Variables Dim oApp As Inventor.Application Dim oDoc As Inventor.Document Dim oSel As Inventor.SelectSet Dim strViewName As String Dim oRepDoc As DrawingDocument Dim oRepView As View ' Initialize the variables Set oApp = ThisApplication Set oDoc = oApp.ActiveDocument Set oSel = oDoc.SelectSet 'If only one part is selected, name the view the same as the part selected If oSel.Count = 1 Then strViewName = oSel(1).Name Else 

Mobile app development for enterprise: The app development should start with a planning process in order to identify the need for the app, desired features and functionality, stakeholder involvement, project timeline, budget considerations, and other key elements. After planning is complete, the app development process begins, which can include designing the interface, coding, testing, and launching the app. AI features can then be integrated into the app, such as natural language processing, automated image recognition, and predictive analytics.

0 Likes
Message 3 of 9

A.Acheson
Mentor
Mentor

Hi @to_schroeder 

 

For this if your only starting out in learning coding in inventor I would suggest to start with ilogic which uses VB.NET behind the scenes. If anything it can be much easier than VBA and this forum provides alot of examples. You will gain access to all the ilogic functions and methods however your request need the full API to carry out. 

 

The API has lots of samples written in VBA so you will probably be familiar with its layout. Unfortuantley there is none dealing with the common task of view rep creation. 

Here is a sample post that covers most of your request inside in one ilogic rule. I suggest to study its contents and try and get it to run. If you need any help post the error messages and I'm sure users can assist.. 

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 4 of 9

WCrihfield
Mentor
Mentor

Hi @to_schroeder.  Here is some code you could copy and paste into an iLogic rule.  When you have an assembly open, and have one or more components selected, then run this rule, it should do what you are asking for.  I haven't tested this version yet, but I had something similar before, and edited it to meet the needs you mentioned here.

 

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting rule.", vbCritical, "iLogic")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oSS As Inventor.SelectSet = oADoc.SelectSet
If oSS.Count = 0 Then
	MsgBox("No assembly components were pre-selected.  Exiting rule.", vbCritical, "iLogic")
	Exit Sub
End If
Dim oOccsCol As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
For Each oObj In oSS
	If TypeOf oObj Is ComponentOccurrence Then
		oOccsCol.Add(oObj)
	End If
Next
Dim sViewName As String
If oOccsCol.Count = 0 Then
	MsgBox("No assembly components were pre-selected.  Exiting rule.", vbCritical, "iLogic")
	Exit Sub
ElseIf oOccsCol.Count = 1 Then
	Dim oOcc As ComponentOccurrence = oOccsCol.Item(1)
	sViewName = oOcc.Name
Else 'more than one
	sViewName = InputBox("Enter View Rep Name.", "View Rep Name", "")
	If sViewName = "" Then Exit Sub 'no name was specified, so exit rule
End If
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oRepsMgr As RepresentationsManager = oADef.RepresentationsManager
Dim oDVRs As DesignViewRepresentations = oRepsMgr.DesignViewRepresentations
Dim oDVR As DesignViewRepresentation = Nothing
Try
	oDVR = oDVRs.Item(sViewName)
Catch
	oDVR = oDVRs.Add(sViewName)
End Try
oDVR.Activate
oDVR.HideAll
oDVR.SetVisibilityOfOccurrences(oOccsCol, True)
oDVR.Locked = True
oADoc.Update
'oADoc.Save

 

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)

Message 5 of 9

to_schroeder
Contributor
Contributor

this solved everything except 1 thing. As i have run it, it didnt go back to the original representation view. How can i edit this in?

Also, if you dont mind explaining, how can i make this rule global and can i set a global hotkey for this rule?

0 Likes
Message 6 of 9

WCrihfield
Mentor
Mentor
Accepted solution

Sorry, I did apparently forget that step.  I simply added a line of code to capture the originally active DVR to a variable, before activating the 'other' one.  Then near the end, after locking that 'other' DVR, I simply use that previously created variable, to activate that originally active DVR again.  To use this code in an external iLogic rule, instead of within an internal iLogic rule, you can simply right click on the internal iLogic rule in your 'Rules' tab, and choose 'Copy', then activate your 'External Rules' tab, then paste that rule where ever you want it within that tab's area.  It should still work just as good that way.

If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("An Assembly Document must be active for this rule to work. Exiting rule.", vbCritical, "iLogic")
	Exit Sub
End If
Dim oADoc As AssemblyDocument = ThisDoc.Document
Dim oSS As Inventor.SelectSet = oADoc.SelectSet
If oSS.Count = 0 Then
	MsgBox("No assembly components were pre-selected.  Exiting rule.", vbCritical, "iLogic")
	Exit Sub
End If
Dim oOccsCol As ObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection
For Each oObj In oSS
	If TypeOf oObj Is ComponentOccurrence Then
		oOccsCol.Add(oObj)
	End If
Next
Dim sViewName As String
If oOccsCol.Count = 0 Then
	MsgBox("No assembly components were pre-selected.  Exiting rule.", vbCritical, "iLogic")
	Exit Sub
ElseIf oOccsCol.Count = 1 Then
	Dim oOcc As ComponentOccurrence = oOccsCol.Item(1)
	sViewName = oOcc.Name
Else 'more than one
	sViewName = InputBox("Enter View Rep Name.", "View Rep Name", "")
	If sViewName = "" Then Exit Sub 'no name was specified, so exit rule
End If
Dim oRepsMgr As RepresentationsManager = oADoc.ComponentDefinition.RepresentationsManager
'record originally active DVR here, so we can re-activate it later
Dim oActiveDVR As DesignViewRepresentation = oRepsMgr.ActiveDesignViewRepresentation
Dim oDVRs As DesignViewRepresentations = oRepsMgr.DesignViewRepresentations
Dim oDVR As DesignViewRepresentation = Nothing
Try
	oDVR = oDVRs.Item(sViewName)
Catch
	oDVR = oDVRs.Add(sViewName)
End Try
oDVR.Activate
oDVR.HideAll
oDVR.SetVisibilityOfOccurrences(oOccsCol, True)
oDVR.Locked = True
oActiveDVR.Activate 're-activate originally active DVR
oADoc.Update
'oADoc.Save

By the way, if you have not used external iLogic rules before, you may want to set up a few things first.  To do so, go to your Tools tab, and there should be a tool named 'iLogic Configuration' on the Options panel.  If you do not see it, you may have to expand the panel down to see it.  In the upper area of that dialog is where you can specify where you want your external iLogic rules to be stored.  I have one main folder for external iLogic rules, which has a bunch of sub folders.  Each sub folder is for a different category of external iLogic rules, for our internal organization, because we have tons of them.  And I specify each of those sub folders in that dialog.  Once that is done, that is where iLogic will first look for any external iLogic rules you attempt to run, so you only need to specify the external rules file name (without extension), and no path.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 9

to_schroeder
Contributor
Contributor
you are a legend! thank you!

is there a way to bind a keyboard shortcut to a rule? So for examblde alt+y runs the rule? does it work like that?
0 Likes
Message 8 of 9

WCrihfield
Mentor
Mentor

Hi @to_schroeder.  No, there is not a way to bind a keyboard shortcut to run a specific iLogic rule...at least not in any simple or direct way.  There is a fairly complicated and advanced way that I believe it could be set up though.  Before the latest versions of Inventor made it possible to add iLogic rules to the RibbonPanels as buttons, there was a way to make that happen using Inventor's API anyways.  And in that process, you create a ButtonDefinition (a type of ControlDefinition), which was then used to add a button (a type of CommandControl) into a RibbonPanel.  The ButtonDefinition that you create in this process has two methods called Execute & Execute2, one of which will be ran when you click the button.  And the ButtonDefinition also has properties allowing you to specify shortcuts for executing it.  Well, in this case, those Execute methods have nothing to do until you create your own custom event handler block of code that will listen for that OnExecute Event, then react to it by running some additional code which is used to run the iLogic rule.  It can be pretty complex and advanced for someone just getting into coding for Inventor.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 9

Frederick_Law
Mentor
Mentor

Addin can create and add new command to IV which will should up in "Customize User Command".

Here is my COG feature Addin:

COGAddin-01.jpg

 

The COG code might take 30 minutes to do.

Everything else for the Addin might be half a day, UI, event, new icon.

0 Likes