I've been trying to make a combo box on a ribbon panel and I've got it working in a very basic mode:
mDwgCombo = mCtrlDefs.AddComboBoxDefinition("Current DimStyle", _ "JZComboDS", _ CommandTypesEnum.kShapeEditCmdType, _ 100, _ strAddInGuid, _ "Current Dim Style", _ "Displays current Dim Style", _ mIPicturedispDSsm, _ mIPicturedispDS, _ ButtonDisplayEnum.kDisplayTextInLearningMode) mDwgCombo.AddItem("X", 0) mDwgCombo.AddItem("Y", 0) mDwgCombo.ListIndex = 1
That loads up just fine. There's no event attached to it at this point, however upon loading an idw file, i get a combo box in the menu with 2 choices, X and Y with a default choice of X. That's fine.
However, I don't want X and Y in the list, i'd like to populate it with the list of existing styles. I have a pop up form that does this, but am trying to convert to a combo box on the ribbon. I tried the following, but it kills the whole addin.
Dim oDrgDoc As DrawingDocument oDrgDoc = g_inventorApplication.ActiveDocument Dim oDrgStlMgr As DrawingStylesManager oDrgStlMgr = oDrgDoc.StylesManager Dim i As Integer Dim n As String For i = 1 To oDrgStlMgr.StandardStyles.Count n = CStr(oDrgStlMgr.StandardStyles.Item(i).Name) mDwgCombo.AddItem(n, 0) Next
Any ideas what I need to do to get the list in the combo box?
(FWIW this in the Standard AddInServer.vb file)
thanks
Mike
How is "mDwgCombo" defined in the second set of code?
Are you setting it to the combo box control before trying to add items to it?
In that method you may need to do something like.
Dim mDwgCombo as ComboBoxDefinition 'Get the Combobox from the Command Manager from Inventor mDwgCombo = g_inventorApplication.CommandManager.ControlDefinitions["JZComboDS"] 'Clear and Add new Items mDwgCombo.Clear() 'Empties ComboBox Dim oDrgStlMgr As DrawingStylesManager oDrgStlMgr = oDrgDoc.StylesManager Dim i As Integer Dim n As String
For i = 1 To oDrgStlMgr.StandardStyles.Count
n = oDrgStlMgr.StandardStyles.Item(i).Name
mDwgCombo.AddItem(n)
Next
Here are those lines:
Private mDwgCombo As ComboBoxDefinition Dim mCtrlDefs As ControlDefinitions = g_inventorApplication.CommandManager.ControlDefinitions mDwgCombo = mCtrlDefs.AddComboBoxDefinition("Current DimStyle", _ "JZComboDS", _ CommandTypesEnum.kShapeEditCmdType, _ 100, _ strAddInGuid, _ "Current Dim Style", _ "Displays current Dim Style", _ mIPicturedispDSsm, _ mIPicturedispDS, _ ButtonDisplayEnum.kDisplayTextInLearningMode)
Can you do mDwgCombo = add definition and later have a mDwgCombo = g_inventorApplication.CommandManager.ControlDefinitions["JZComboDS"] ?
Apart from that, the only difference i see is you're adding a mDwgCombo.Clear() option before adding anything to it. Plus I still need to dim ODrgDoc, right?
What methods are you in when you create the combobox and later try and add the drawing styles?
not sure what you mean by methods...Attached is the whole StandardAddInServer.vb file. Hopefully what you're asking is in there.
As noted above, i define the button from the command.
Then:
Dim DwgStyleMan as Inventor.DrawingStylesManager
DwgStyleMan = g_inventorApplication.ActiveDocument.StylesManager
On the DwgStyleMan it kills the whole addin. No error message, just on loading inventor the plug flat out does not load because of that line. (I comment out that line and the plug loads). Not sure what the issue is. There is an active document and it does have styles and can use the styles manager. As i said, the code works fine if it's in a form. I'm just trying to get it on a pull down in the ribbon instead.
The method that your are defining the ComboBox defintion in is the Activate method. In an Add-In this is the first method that Inventor calls to load and set up your add-in. By default this will happen right after Inventor has loaded but before any documents are opened.
The reason setting the values of the combo box to drawing styles is failing is there is no open drawing at that moment. Chances are there is no open active document at all. So the line ( InvDoc7 = g_inventorApplication.ActiveDocument ) sets InvDoc7 to an null or empty value. When you attempt to read from it in the line ( DwgStyleMan = InvDoc7.StylesManager ) you are trying to get the StylesManager of an object that doesn't exist. So it crashes.
To do want you want to do you either have to preset it to a known list of values, dynamically change the value list dependent on the styles of the currently opened drawing, or after Inventor has fully loaded in the background open the drawing template file, set the combobox to the styles and close the template file. Setting the styles to the currently opened drawing is probably best. A static list is the easiest.
That makes sense.
In a perfect world i'd like it to dynamically update the list based on the open drawing. Not entirely sure how to go about that offhand....i'd guess somethng like, check for an existing drawing - if no drawing, do a manual list, else get list from file...
thanks for helping me clarify that. Not sure why that never occurred to me, but it's good to know now 😉
After you add the handlers for your buttons add this handler.
AddHandler m_inventorApplication.ApplicationEvents.OnActivateView, AddressOf ActivateViewEvent
Then after the the Activate method add a new method that looks like this. This will be similar to the button event methods.
Private Sub ActivateViewEvent(ViewObject As Inventor.View, BeforeOrAfter As Inventor.EventTimingEnum, _ Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) If Not g_inventorApplication.ActiveDocument Is Nothing And BeforeOrAfter = EventTimingEnum.kAfter Then Dim InvDoc7 As Inventor.DrawingDocument InvDoc7 = TryCast(g_inventorApplication.ActiveDocument, Inventor.DrawingDocument) If Not InvDoc7 Is Nothing Then Dim oDrgStlMgr As Inventor.DrawingStylesManager oDrgStlMgr = InvDoc7.StylesManager Dim i As Integer Dim idx As Integer Dim n As String For i = 1 To oDrgStlMgr.StandardStyles.Count n = oDrgStlMgr.StandardStyles.Item(i).Name mDwgCombo.AddItem(n, 0) If oDrgStlMgr.ActiveStandardStyle.Name = n Then idx = n End If Next If idx > 0 And idx <= mDwgCombo.ListCount Then mDwgCombo.ListIndex = idx End If End If End If HandlingCode = HandlingCodeEnum.kEventHandled End Sub
With this handler after a view is activated if it is a drawing view it will update the drawing combo box to match the current document.
(Haven't tested it but should get you close.)
What it does is after a view is activated looks to see if has any opened drawings and are they drawings. If so it updates the combobox with the styles of the drawing and sets it to the current style if one is selected.
Ok, this is weird.
It's working, but doing something bizarre. Here's the code as it stands now:
If Not InvDoc7 Is Nothing Then Dim oDrgStlMgr As Inventor.DrawingStylesManager oDrgStlMgr = InvDoc7.StylesManager mDwgCombo.Clear() Dim i As Integer Dim idx As Integer Dim n As String 'MsgBox(CStr(oDrgStlMgr.StandardStyles.Count)) For i = 1 To oDrgStlMgr.StandardStyles.Count n = oDrgStlMgr.StandardStyles.Item(i).Name mDwgCombo.AddItem(n, 0) If oDrgStlMgr.ActiveStandardStyle.Name = n Then idx = i End If Next 'If idx > 0 And idx <= mDwgCombo.ListCount Then ' mDwgCombo.ListIndex = idx 'End If End If
I rand it as you wrote it and it worked more or less. the problem, though, was it was repeating the first 7 styles about 10 times (70 items in the list). I read the code repeatedly and couldn't find a reason for it, so i commented out all the stuff about setting the idx value - that being the last two if/then bits. Upon running that, i got a correct list of 14 styles.
I kept reading it and from there it basically says if this style is the current style set IDX to it's id number and then the final if/then says set the default choise to that ID number. Makes sense to me.
I did notice that the IDX needs set to a integer and n is the text name of the style, so i used i instead of n. Running that It does report the correct integer for the default choice, however, it puts the full list and then repeats the last 7 entries about 10 times. The first example repeated the first 7... I'm really failing to see how a bit that basically says if this bit of text = that bit of text, set this number 7 causes the loop to get screwed up like that. And this is all still with the last if/then setting the listindex commented out.
I just commented out the idx=i line - leaving the if/then syntax with nothing between it but a 'do nothing (just a comment, not code) and it creates the normal 14 item list it should. So, the issue is in idx = i it seems...I changed idx to LX and it seems to be working. I'm not using idx anywhere else, and VS isn't reporting errors with it, but it is what is...not sure what it'd be. However, the whole thing is working as intended now. 🙂
Thanks!
Now to add an OnChange event and it should be good to go!
Glad you got it working. That is weird the variable idx didn't work correctly. The variable name should affect it.
Yes (idx = i) was the correct form. Two other slight changes two could be to change i and LX to Longs. And in the loop at the end put "Next i" instead of just "Next"
Also when you respond to OnSelect for the comboBox be sure to check if the current selection is equal to the current style so it doesn't do extra work.
I seem to be having trouble getting the selected entry in the OnChange event. Can't seem to reference the combo box. I'm guessing i somehow have to send the choice via the handler, but i've not run into an example that does that. The OnChange event presently just has the ByVal Context as Inventor.NameValueSpace in it, which doesn't seem to be sending the name or id.
OnChange or OnSelect event of the comboBox. Should be OnSelect. Use your original definition object to get the current value.
Inside the handler method you can get the current value of the comboBox using
value = mDwgCombo.ListItem[mDwgCombo.ListIndex]
I tend to use this method to get the current value because I've had issues with quotes and quotations using mDwgCombo.Text. Also you can test the ListIndex to make sure there's something selected. If ListIndex is less than 1 than nothing is selected.