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: 

Use "Measure Distance" Tool from VBA Form?

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
benemt
2514 Views, 8 Replies

Use "Measure Distance" Tool from VBA Form?

I have a VBA form coded that we use to generate part numbers and descriptions that then populate the iProperties.  We often use the part sizes in the description.  I would like to be able to put a button on the form that allows users to use the "Measure Distance" tool so that they can measure the part while they are creating the description. 

 

Does anyone know code that I could use to activate the "measure" tool while a vba form is active?

 

Using Inventor 2012.

 

Thanks.

8 REPLIES 8
Message 2 of 9
Anonymous
in reply to: benemt

Hi benemt,

 

You should be able to run the Measure tool with the following code in VBA:

 

Sub Main()

 

Dim app As Inventor.Application
Set app = ThisApplication
Dim partDoc As PartDocument
Set partDoc = app.ActiveDocument

Dim oControlDef As ControlDefinition


Set oControlDef = app.CommandManager.ControlDefinitions.Item("AppMeasureDistanceCmd")
oControlDef.Execute

 

End Sub

 

 

Regards

Mitch

 

Let me know if it helps, Kudos if it works! 

Message 3 of 9
benemt
in reply to: Anonymous

Thanks!  This worked with one modification.  Because I am using it within a user form, I need to Hide the user form while I execute the command in order to let the user take the measurement.  Then, I show the form once the "Measure Distance" dialog box has been closed.  This required the .Execute2(True) to be used. More info on Execute2 here: http://modthemachine.typepad.com/my_weblog/2009/03/running-commands-using-the-api.html

 

 

Private Sub cmdMeasure_Click()

    Dim app As Inventor.Application
    Set app = ThisApplication
    Dim partDoc As PartDocument
    Set partDoc = app.ActiveDocument

    Dim oControlDef As ControlDefinition


    Set oControlDef = app.CommandManager.ControlDefinitions.Item("AppMeasureDistanceCmd")
    
    frmWizard.Hide
    
    oControlDef.Execute2 (True)
    
    frmWizard.Show

End Sub

 

Message 4 of 9
Anonymous
in reply to: benemt

By default, the form is set to modal, if you change the behaviour of the form, you can measure without the need to deactivate the form

Message 5 of 9
benemt
in reply to: Anonymous


@Anonymous wrote:

By default, the form is set to modal, if you change the behaviour of the form, you can measure without the need to deactivate the form


Another good idea,  thanks Frederic!

 

Now that this is working very well, I would ideally like to be able to take the number found in the Measure Distance box and be able to insert it into my form.  Based on what I've read about running these sorts of commands using the API, I don't think I can get access to this information but I figured I would throw it out there to see if anyone has an idea on that one.

 

Message 6 of 9
Anonymous
in reply to: benemt

Hi benemt,

 

I haven't worked with these yet, but I believe what you're looking for are the "Attributes" of the part. If I'm understanding correctly as I read through threads every so often, you can programmically access lengths this way. There would also be no need to measure the line manually.

 

I'm not sure if you know which distance you'd like to measure beforehand, or if it is completely dependant on what the user would like. 

 

Again, I haven't worked with these yet and not even sure if I'm on the right track, but I would start there if it helps you.

 

Regards

Mitch

Message 7 of 9
benemt
in reply to: Anonymous


@Anonymous wrote:

Hi benemt,

 

I haven't worked with these yet, but I believe what you're looking for are the "Attributes" of the part. If I'm understanding correctly as I read through threads every so often, you can programmically access lengths this way. There would also be no need to measure the line manually.

 

I'm not sure if you know which distance you'd like to measure beforehand, or if it is completely dependant on what the user would like. 

 

Again, I haven't worked with these yet and not even sure if I'm on the right track, but I would start there if it helps you.

 

Regards

Mitch


Interesting thought, Mitch.  Thanks.  It is dependent on what the user would like but I could populate a list of dimensions in the part for them to pick from.  I think I will let this idea sit on the shelf for now and possibly develop it for the next update once I've been able to think about it for a bit.

Message 8 of 9
Anonymous
in reply to: Anonymous

And the help sample file???

 

Public Sub InteractiveMeasureDistance()

    ' Create a new clsMeasure object.
    Dim oMeasure As New clsMeasure

    ' Call the Measure method of the clsMeasure object
    Call oMeasure.Measure(kDistanceMeasure)

End Sub


Public Sub InteractiveMeasureAngle()

    ' Create a new clsMeasure object.
    Dim oMeasure As New clsMeasure

    ' Call the Measure method of the clsMeasure object
    Call oMeasure.Measure(kAngleMeasure)
End Sub

'*************************************************************
' The declarations and functions below need to be copied into
' a class module whose name is "clsMeasure". The name can be
' changed but you'll need to change the declaration in the
' calling function "InteractiveMeasureDistance" and
' "InteractiveMeasureAngle" to use the new name.

' Declare the event objects
Private WithEvents oInteractEvents As InteractionEvents
Private WithEvents oMeasureEvents As MeasureEvents

' Declare a flag that's used to determine when measuring stops.
Private bStillMeasuring As Boolean
Private eMeasureType As MeasureTypeEnum

Public Sub Measure(MeasureType As MeasureTypeEnum)
   
    eMeasureType = MeasureType
  
    ' Initialize flag.
    bStillMeasuring = True

    ' Create an InteractionEvents object.
    Set oInteractEvents = ThisApplication.CommandManager.CreateInteractionEvents

    ' Set a reference to the measure events.
    Set oMeasureEvents = oInteractEvents.MeasureEvents
    oMeasureEvents.Enabled = True
    
    ' Start the InteractionEvents object.
    oInteractEvents.Start
    
    ' Start measure tool
    If eMeasureType = kDistanceMeasure Then
        oMeasureEvents.Measure kDistanceMeasure
    Else
        oMeasureEvents.Measure kAngleMeasure
    End If
    
    ' Loop until a selection is made.
    Do While bStillMeasuring
        DoEvents
    Loop

    ' Stop the InteractionEvents object.
    oInteractEvents.Stop

    ' Clean up.
    Set oMeasureEvents = Nothing
    Set oInteractEvents = Nothing
End Sub

Private Sub oInteractEvents_OnTerminate()
    ' Set the flag to indicate we're done.
    bStillMeasuring = False
End Sub

Private Sub oMeasureEvents_OnMeasure(ByVal MeasureType As MeasureTypeEnum, ByVal MeasuredValue As Double, ByVal Context As NameValueMap)
    
    Dim strMeasuredValue As String
    
    If eMeasureType = kDistanceMeasure Then
        strMeasureValue = ThisApplication.ActiveDocument.UnitsOfMeasure.GetStringFromValue(MeasuredValue, kDefaultDisplayLengthUnits)
        MsgBox "Distance = " & strMeasureValue, vbOKOnly, "Measure Distance"
    Else
        strMeasureValue = ThisApplication.ActiveDocument.UnitsOfMeasure.GetStringFromValue(MeasuredValue, kDefaultDisplayAngleUnits)
        MsgBox "Angle = " & strMeasureValue, vbOKOnly, "Measure Angle"
    End If
    
    ' Set the flag to indicate we're done.
    bStillMeasuring = False
    
End Sub
Message 9 of 9
benemt
in reply to: Anonymous

Thanks again Frederic.  I'm quite busy right now but I will try this out as soon as I get a bit of time.  Based on what I had read, I thought I was going to need more of a workaround so I didn't even think to look at the help file.

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

Post to forums  

Autodesk Design & Make Report