Insert Drawing Note at Mouse Click (x,y) Location - Total WT of Ref Component in Drawing View

Insert Drawing Note at Mouse Click (x,y) Location - Total WT of Ref Component in Drawing View

cameron.houston
Enthusiast Enthusiast
1,810 Views
16 Replies
Message 1 of 17

Insert Drawing Note at Mouse Click (x,y) Location - Total WT of Ref Component in Drawing View

cameron.houston
Enthusiast
Enthusiast

Hi All,

 

I am creating a macro, ultimately a button down the line, that allows me to: 1. click on a drawing view, 2. retrieve the reference component weight, 3. insert a text note listing the weight at a specified location. I currently have code that does 1 & 2 and part of 3. I am getting hung up on placing the fitted note at a specified location. Ultimately I want the UI to go like this: Prompt me to select a weight, Prompt me to select a location for the note, note appears. Below is the code I currently have. FYI I have a couple message boxes in play now just to make sure I am receiving the right values.

 

 

**Additionally, bonus points for telling me how to round my lb mass to the nearest 1lb. **

 

Public Sub TotalWT()


Debug.Print ThisApplication.Caption

'define active document as drawing doc. Will produce an error if its not a drawing doc
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument

'Set sheet to be active sheet in window
Dim oSheet As Sheet
Set oSheet = oDrawDoc.ActiveSheet

'Get the desired view via user mouse click
Dim oDrawingView As DrawingView
Set oDrawingView = ThisApplication.CommandManager.Pick(kDrawingViewFilter, "Select the drawing view")


'Retrieve the mass of the drawing views referenced part
oMassKG =oDrawingView.ReferencedDocumentDescriptor.ReferencedDocument.ComponentDefinition.MassProperties.Mass

 

'convert to lb mass
oMassLbs = oMassKG * 2.20462


MsgBox (" The Mass of the Item is " & oMassLbs & "lbmass")

'Define something to store x y point
Dim oTG As TransientGeometry
***Iknow somewhere in here is where I need to assign my mouse click points to my transient geo but can figure out how to capture mouse click x,y***

 

'Create the general note

Dim oGeneralNote As GeneralNote

 

'this is where I need to add my points as x, y
Set oGeneralNote = oSheet.DrawingNotes.GeneralNotes.AddFitted((0 14),"Total WT:" & oMassLbs)




End Sub

0 Likes
1,811 Views
16 Replies
Replies (16)
Message 2 of 17

mcgyvr
Consultant
Consultant

Getting mouse position

https://forums.autodesk.com/t5/inventor-customization/get-mouse-position-using-ilogic/td-p/7713533

 

Rounding to whole numbers 

Round(oMassLBS)

https://knowledge.autodesk.com/support/inventor-products/learn-explore/caas/CloudHelp/cloudhelp/2015...

 



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
0 Likes
Message 3 of 17

cameron.houston
Enthusiast
Enthusiast

Thanks for the help with the rounding, looks like that was an easy one. For the mouse click one, I have seen that before, I am having trouble running it though. I am using VBA right now to macro this and the ILogic runs something similar to VB.net. I added the necessary "sets", and changed the message box but I am getting an error " Compile Error: Sub or Function not defined" on the "AddHandler".  Here is my code as of now:

 

Sub Main()
 Dim oInteraction  As InteractionEvents
 Set oInteraction = ThisApplication.CommandManager.CreateInteractionEvents
 Dim oMouse As MouseEvents
 Set oMouse = oInteraction.MouseEvents
  AddHandler oMouse.OnMouseClick, AddressOf oMouse_OnMouseDown
  oInteraction.Start
 
End Sub
Sub oMouse_OnMouseDown(Button As MouseButtonEnum, ShiftKeys As ShiftStateEnum, ModelPosition As Point, ViewPosition As Point2d, View As Inventor.View)
 
  MsgBox (" The Mass of the Item is " & ModelPosition.X & " x pos")
  MsgBox (" The Mass of the Item is " & ModelPosition.Y & " y pos")
 
End Sub

 

0 Likes
Message 4 of 17

WCrihfield
Mentor
Mentor

I don't think the AddHandler line works in VBA.

I think you have to declare the two variables for the events this way, outside of the procedure (after End Sub):

 

Private WithEvents oInteraction As Inventor.InteractionEvents
Private WithEvents oMouse As Inventor.MouseEvents

 

Then just set their values for the two variables within the procedure.

Then, at the end of the "Sub oMouse_OnMouseDown" initial line, after the closing ")", put a single space then "Handles oMouse.OnMouseClick" (without the quotes).

 

If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.

If you have time, please... Vote For My IDEAS 💡or you can Explore My CONTRIBUTIONS

Inventor 2021 Help | Inventor Forum | Inventor Customization Forum | Inventor Ideas Forum

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 17

cameron.houston
Enthusiast
Enthusiast
 

Hi @WCrihfield

 

Something like this? I am getting errors and cannot initialize. It is not liking hwo I declared private withevents or the handles.Code.PNG

0 Likes
Message 6 of 17

WCrihfield
Mentor
Mentor

Here is a link to an old post where Brian Ekins created a VBA macro for capturing mouse click location.  Doing it by VBA is much more complicated than with iLogic (vb.net), as you can see.

https://forums.autodesk.com/t5/inventor-customization/selecting-a-point2d-with-your-mouse-on-a-drawi... 

 

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 7 of 17

cameron.houston
Enthusiast
Enthusiast

Yeah it appears to be, I am ultimately looking to take this to a button so it will end up in vb.net. I usually just code in vba until I get it working how I like then port it over. Just for the sake of it though I would like to get this macro running. Cannot get Brians code to work it doesn't like the 2 Private WithEvents lines, gives me a compile error "Only Valid in Object Module"Code.PNG

0 Likes
Message 8 of 17

WCrihfield
Mentor
Mentor

If you read carefully, you'll notice that all his code doesn't go into the same module.  The second block of code he posted is designed to go into a 'Class Module', not a regular module.  Then the first block of code he posted is designed to go into a regular module, as you would a regular macro.  The regular macro then calls/uses on the methods and events created within the Class Module.  I have a slightly different code than his for both code blocks, but for the most part, it does the same thing.   It will only allow you to define events within class modules, because they are part of the (what is the object, what methods does it have, what properties does it have, and what events does it have) involved in creating a custom class (object).  As far as defining events, VB.NET (& iLogic) can use both AddHandler and WithEvents, but VBA can only use WithEvents.

I hope this clears some of that up for you. 🙂

 

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 9 of 17

cameron.houston
Enthusiast
Enthusiast

@WCrihfield 

 

Say I give up on the macro side of things. Do you know how I could get the mouse click to work in visual studios vb for an addin? Can I just copy and paste the illogic into my vb source code?

0 Likes
Message 10 of 17

WCrihfield
Mentor
Mentor

For the most part, yes, the iLogic version should translate nicely into Visual Studio or Add-in.  But you will likely have to modify a few things.

For instance, you will likely want to change "Sub Main" to something different, and depending on your needs/expectations, you may even want to change it to a Function.  As you may already know, a Sub basically just does something, while a Function processes something and returns a result.  If you just want MsgBox type results, you can stick with the Sub, but if you want the routine to return a 'Point' or 'Point2d' type object, you would need to set it up as a Function.  For instance, you could set-up a main Function called "Public Function GetMouseClickPosition() As Point", and have it run the "Sub Main" (after changing Main to something else), then have it run the "Sub oMouse_OnMouseDown()", but change Sub to Function, then instead of a MsgBox, have it return the "ModelPosition" variable (Point) to the parent Function, then have the main function also return a Point type variable to its calling routine.  (I hope all that makes sense to you. 🙂)

And of course, "ThisApplication" may need to be either specifically defined, or replaced with a variable holding the 'Inventor.Application' object, which you retrieved beforehand, because that term is defined within the iLogic add-in.

Another thing to keep in mind is that you may need to put "Inventor." before some of the Types you are specifying, to avoid confusion with similarly named objects within other resource references.  For instance the 'Point' object...some other 'references' and 'imports' also have a 'Point' object in them, but they mean something different, and have different properties/methods.

 

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 11 of 17

cameron.houston
Enthusiast
Enthusiast

Okay I will give this a try. Another question I will throw out is: Do you know a way to insert not just the static value of mass into the text box but the dynamic value of mass? IE it will update when the mass changes? Just like when you use physical properties in a text box.

0 Likes
Message 12 of 17

WCrihfield
Mentor
Mentor

Yes. It is definitely doable. But what type of text box are we talking about here?  For example:  Inventor.TextBox (in a sketch), Inventor.TextBox (in on a drawing sheet), System.Windows.Forms.TextBox (in a Windows Form).

When inserting it into a drawing note or inventor text box, you must use its' FormattedText property, and XML tags.

When inserting a 'live reference' from one Inventor.Property into another Inventor.Property, you need to do so by its Property.Expression, as a String, starting with "=", then the exact name of the other property within the "<" & ">" brackets, as you see them when inserted.

I'm not exactly sure how you would include a 'live reference' to an inventor property within a Windows form text box, other than simply assigning the value of the property to the TextBox.Text property.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 13 of 17

cameron.houston
Enthusiast
Enthusiast

Hi  @WCrihfield ,

 

I am looking to create a 'live reference' within a drawing text box (formatted text box). I can do this by assigning a property set value? How do I go about that? Would it be possible for you to provide an example of this? The living property I am looking for is part number and weight.

 

0 Likes
Message 14 of 17

cameron.houston
Enthusiast
Enthusiast

@WCrihfield 

 

For reference

 

Public Sub DetailItemNumber()
Debug.Print ThisApplication.ActiveDocument
    'Get the active document which must be a drawing or else it will error
    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument
   
    'Create an object to store all the sheets called "oSheets"
    Dim oSheets As Sheets
    Set oSheets = oDrawDoc.Sheets
   
    Count = 0
   
    Dim Viewcount As Integer
    Viewcount = 0
   
    Dim d As Integer
    d = 0
    'Create an object to store an individual sheet
    Dim oSheet As Sheet
       
        'For every sheet in drawing doc
        For Each oSheet In oSheets
       
            Count = Count + 1
           
            'Create an object to store all the drawing views
            Dim oViews As DrawingViews
            Set oViews = oSheet.DrawingViews
           
            'Create an object to store an individual view
            Dim oView As DrawingView
               
                'For ever drawing view on the sheet
                For Each oView In oViews

                    Dim c As Integer
                    c = 0
                    'Get the part number associated with the drawing view
                    Dim oPartNumber As String
                    oPartNumber = oView.ReferencedDocumentDescriptor.ReferencedDocument.PropertySets.Item(3).Item(2).Value
 
 
 
^^^^^^^^ Above is how I am currently accessing the inventor part number property. This results in  a static value. How would I convert this to a living reference?
0 Likes
Message 15 of 17

WCrihfield
Mentor
Mentor

When creating the formatted text needed to create a 'live' reference to a part number iProperty, for putting in a drawing note, you must specify either "drawing" or "model" (literally, not a specific document variable).  By "model" it means the first model being represented in the first drawing view that was placed in the drawing, because drawings were originally meant to be documenting one model document (either a part, or an assembly, but just one model document).  When you use the document reference "ThisDrawing.ModelDocument", it is getting that 'first' model document, no matter how many other model documents there may be represented within the drawing.  So, there's no need to get the part number down within a loop of all drawing views, because it won't do any good.  If your drawing is representing multiple model documents (different model documents being shown in different views), then adding this type of 'live' reference won't work for you, because it would always be pointing to the same (first model) document, instead of the document being represented within the view.

 

Anyways, here is a very simple example of creating a drawing general note, and putting a 'live' reference of 'the model' part number in it.  As you can see in the line defining oFormattedText, the document variable is literally set to 'model', then the FormatID is setting which property set, then PropertyID is setting which property.

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
	MsgBox("A Drawing Document must be active for this rule to work. Exiting.",vbOKOnly+vbCritical, "WRONG DOCUMENT TYPE")
	Exit Sub
End If
Dim oDDoc As DrawingDocument = ThisDrawing.Document
Dim oSheet As Inventor.Sheet = oDDoc.ActiveSheet
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oInsPoint As Point2d = oTG.CreatePoint2d(oSheet.Width / 2, oSheet.Height / 2) 'center of sheet
Dim oFormattedText As String = "<Property Document='model' FormatID='{32853F0F-3444-11d1-9E93-0060B03C1CA6}' PropertyID='5' />"
Dim oNote As GeneralNote = oSheet.DrawingNotes.GeneralNotes.AddFitted(oInsPoint, oFormattedText)

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)

0 Likes
Message 16 of 17

cameron.houston
Enthusiast
Enthusiast

Hi @WCrihfield 

I see what you are saying about the .ModelDocument always being the first view you slap down, but how come every view can have a view label that is associated to the view and not the first model( ie you have 3 view (A,B,C) each one gets a unique live reference view label)? If that is possible why is it not possible to tap into that same functionality through the API. 

 

0 Likes
Message 17 of 17

cameron.houston
Enthusiast
Enthusiast

@WCrihfield 

 

When I used the kernel you supplied and put it into my code it actually works exactly as I wanted it to. I think because I am calling it within the View Loop it grabs the model properties of the view not of the first view down or the drawing. The code below gives me dynamically updating values of Part Number & Total WT.

 

 

 

Public Sub DetailItemNumber()

Debug.Print ThisApplication.ActiveDocument

'Get the active document which must be a drawing or else it will error
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument

'Create an object to store all the sheets called "oSheets"
Dim oSheets As Sheets
Set oSheets = oDrawDoc.Sheets

Count = 0

Dim Viewcount As Integer
Viewcount = 0

Dim d As Integer
d = 0
'Create an object to store an individual sheet
Dim oSheet As Sheet

'For every sheet in drawing doc
For Each oSheet In oSheets

Count = Count + 1

'Create an object to store all the drawing views
Dim oViews As DrawingViews
Set oViews = oSheet.DrawingViews

'Create an object to store an individual view
Dim oView As DrawingView

'For ever drawing view on the sheet
For Each oView In oViews
Viewcount = Viewcount + 1
'initialize a counter to be used later
Dim c As Integer
c = 0
'Get the part number associated with the drawing view
Dim oPartNumber As String
oPartNumber = oView.ReferencedDocumentDescriptor.ReferencedDocument.PropertySets.Item(3).Item(2).Value

Dim osText As String
osText = "<Property Document='model' FormatID='{32853F0F-3444-11d1-9E93-0060B03C1CA6}' PropertyID='5' />"

Dim oMass As String
oMass = "<PhysicalProperty PhysicalPropertyID='72449' Precision='0'></PhysicalProperty>"
'MsgBox (oMass)
'Testing Message Box
'MsgBox ("Drawing View " + oPartNumber)

'Create an object to store all the boms
Dim oBoms As DrawingBOMs
Set oBoms = oDrawDoc.DrawingBOMs

'Create an object to store an individual bom
Dim oBom As DrawingBOM

'For every bom in the drawing
For Each oBom In oBoms

'Locate where the MPN & Item Columns Are
For i = 1 To oBom.DrawingBOMColumns.Count

'Loop through all the BOM headers to find the columns and then save the column number they are in

oFindMPN = oBom.DrawingBOMColumns.Item(i).Title

If oFindMPN = "MPN" Then
oMPNCol = i

' MsgBox ("MPN IS IN " & oMPNCol)
End If
Next

For i = 1 To oBom.DrawingBOMColumns.Count

oFindItemNum = oBom.DrawingBOMColumns.Item(i).Title

If oFindItemNum = "ITEM" Then
oFindItemNumCol = i

'MsgBox (" ITEM IS IN COLUMN" & oFindItemNumCol)
End If
Next



'Store Bom reference assembly
Dim RefAssemblyLong As String
RefAssemblyLong = oBom.ReferencedDocumentDescriptor.DisplayName

'Count how many characters in RefAssembly
Dim oLengthAssembly As Integer
oLengthAssembly = Len(RefAssemblyLong)

'Assembly name without .iam (4 characters)

Dim RefAssembly As String
RefAssembly = Left(RefAssemblyLong, oLengthAssembly - 4)

'For every row in the BOM
For i = 1 To oBom.DrawingBOMRows.Count

'Get the MPN number for the Bom Item
Dim oMPN As String

oMPN = oBom.DrawingBOMRows.Item(i).Item(oMPNCol).Value

Dim oItem As String
oItem = oBom.DrawingBOMRows.Item(i).Item(oFindItemNumCol).Value

If oView.ViewType = kStandardDrawingViewType And oPartNumber = oMPN Then
oView.Label.FormattedText = ("DETAIL ITEM " & oItem & vbCrLf & osText & vbCrLf & oMass)
GoTo CONT:
'DrawingViewTypeEnum.kDetailDrawingViewType
End If

Next


Next
CONT:

Next
Next

'MsgBox ("Detail Views Have Been Updated")
End Sub

0 Likes