Window Select/Region is Empty on Inventor Drawing with VBA

Window Select/Region is Empty on Inventor Drawing with VBA

Anonymous
Not applicable
2,984 Views
25 Replies
Message 1 of 26

Window Select/Region is Empty on Inventor Drawing with VBA

Anonymous
Not applicable

I am trying to place some text on a drawing through a VBA macro.  The text will be going within the bounding box of a base view and I want to check that there is nothing (model geometry or other annotations) in that location that the text will overlap with.  I will be looking at a strictly rectangular region.

 

I would like to simulate a "window select" operation (typically performed by clicking and dragging the left mouse button in the viewport).  More specifically I would like to simulate a right-to-left (intersect) "window select" operation.  My idea is to use the SelectSet resulting from that operation to determine if any of the object types I'm worried about are present in that region.

 

I cannot find any documentation or forum posts with a way to do a "window select" with VBA.  Is there a way to do this?

 

If not, is there another way to check if a rectangular region on a drawing sheet is free of certain types of entities?  Preferably I would like a list of all the objects in that region so I can do my own filtering operations.

0 Likes
2,985 Views
25 Replies
Replies (25)
Message 2 of 26

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous,

 

Hoping that below API sample link may helpful to your situation.

 

http://help.autodesk.com/view/INVNTOR/2019/ENU/?guid=GUID-60F44C84-D0A6-445B-ADDB-575E3B420DC2

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 3 of 26

Anonymous
Not applicable

The link appears broken.  The page gives me a 404.  I've had this issue before with the online API help.

 

If you can give me the navigation path through the topics, I can find it.

0 Likes
Message 4 of 26

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous,

 

It's working for me. You can also refer the same documentation in Inventor product as shown in below image.

API documentation.png

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 5 of 26

Anonymous
Not applicable

Yes, but is there a specific topic you are referring to?

0 Likes
Message 6 of 26

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous,

 

Search with keyword "Window Selection API Sample".

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 7 of 26

Anonymous
Not applicable

I found the sample and attempted to test it out.  I've read through the code and, while I don't understand a good portion of it, I don't think this is the functionality that I'm looking for.  This appears to modify and filter the user's window selection actions.  I want window selection to be accomplished completely automatically.  The code will need some way for me to specify the start and end 2d points for the selection window.  This doesn't contain any way to perform such definitions.

 

I tried running the code following the instructions in the commented lines and on the API help page, however it doesn't appear to be working.  My understanding from the code is this is to be performed on a part file and will only select circular edges and circular arcs.

0 Likes
Message 8 of 26

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous 

 

Try below VBA code.

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

' Declare the event objects
Private WithEvents oInteractEvents As InteractionEvents
Private WithEvents oSelectEvents As SelectEvents

' Declare a flag that's used to determine if command prompts are shown as tooltips.
Private bTooltipEnabled As Boolean

Public Function WindowSelect()
   ' Create an InteractionEvents object.
   Set oInteractEvents = ThisApplication.CommandManager.CreateInteractionEvents

   ' Ensure interaction is enabled.
   oInteractEvents.InteractionDisabled = False

   ' Set a reference to the select events.
   Set oSelectEvents = oInteractEvents.SelectEvents

   ' Set the filter for circular edges (this includes circular arcs).
   oSelectEvents.AddSelectionFilter kAllEntitiesFilter

   oSelectEvents.WindowSelectEnabled = True

   bTooltipEnabled = ThisApplication.GeneralOptions.ShowCommandPromptTooltips
   ThisApplication.GeneralOptions.ShowCommandPromptTooltips = True

   oInteractEvents.StatusBarText = "Window select. Esc to exit."

   ' Start the InteractionEvents object.
   oInteractEvents.Start
End Function

Private Sub oInteractEvents_OnTerminate()
   ' Reset to original value
   ThisApplication.GeneralOptions.ShowCommandPromptTooltips = bTooltipEnabled

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

Private Sub oSelectEvents_OnPreSelect(PreSelectEntity As Object, DoHighlight As Boolean, MorePreSelectEntities As ObjectCollection, ByVal SelectionDevice As SelectionDeviceEnum, ByVal ModelPosition As Point, ByVal ViewPosition As Point2d, ByVal View As View)
   ' Set a reference to the selected edge.
   ' Only circular edges can come through since the circular edge filter was set.
   'Dim oEdge As Edge
   'Set oEdge = PreSelectEntity

   ' Allow only fully circular edges to be picked.
   'f Not oEdge.GeometryType = kCircleCurve Then
    ' DoHighlight = False
   'End If
End Sub

Private Sub oSelectEvents_OnSelect(ByVal JustSelectedEntities As ObjectsEnumerator, ByVal SelectionDevice As SelectionDeviceEnum, ByVal ModelPosition As Point, ByVal ViewPosition As Point2d, ByVal View As View)
   MsgBox "Picked " & JustSelectedEntities.Count & " objects."
End Sub

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 9 of 26

Anonymous
Not applicable

Like I said before, this does not do what I want it to do.  This just modifies the behavior of the MANUAL window select.  I want to perform the window select operation COMPLETELY through the code.

0 Likes
Message 10 of 26

CadUser46
Collaborator
Collaborator

@Anonymous Just throwing this out there.  You can add any drawing views on the sheet to a selectset and pick up the coords from there.  Or simply just get the coords of symbol and coords of each drawing view, bypass the selectset.


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro
0 Likes
Message 11 of 26

Anonymous
Not applicable

That's not unlike something I've tried in the absence of any solutions through the forum.  Basically I'm collecting all the line segments from the drawing view and creating transient geometry to represent annotations (balloons mostly).  I'm doing position and intersection checks on all of those pieces of geometry to determine if anything lies fully or partially inside the box I specify.

 

This is not a good solution.  It's a couple of hundred lines of code and takes 2-5 MINUTES to check ONE rectangle.  I need to perform this dozens of times per view, per sheet, per drawing.

 

When I click and drag (right-to-left) with the mouse in the viewport I can perform a window select operation that selects any objects that intersect the selection box and it takes only milliseconds (i.e. nothing noticeable to the user) to process.  The window select operation is performing the exact same "collision" checks between a large number of curves and a simple rectangular region of the screen.  There's got to be a way to do a similar rectangular selection operation through code and achieve similar performance.

Message 12 of 26

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous,

 

Can you please describe the same procedure with help of video record or screencast?

 

Please make sure that files are non confidential.

 

Thanks and regards,

 


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 13 of 26

Anonymous
Not applicable

Which procedure are you referring to?  The window select operation?

0 Likes
Message 14 of 26

chandra.shekar.g
Autodesk Support
Autodesk Support

@Anonymous wrote:

That's not unlike something I've tried in the absence of any solutions through the forum.  Basically I'm collecting all the line segments from the drawing view and creating transient geometry to represent annotations (balloons mostly).  I'm doing position and intersection checks on all of those pieces of geometry to determine if anything lies fully or partially inside the box I specify.

 

This is not a good solution.  It's a couple of hundred lines of code and takes 2-5 MINUTES to check ONE rectangle.  I need to perform this dozens of times per view, per sheet, per drawing.

 

When I click and drag (right-to-left) with the mouse in the viewport I can perform a window select operation that selects any objects that intersect the selection box and it takes only milliseconds (i.e. nothing noticeable to the user) to process.  The window select operation is performing the exact same "collision" checks between a large number of curves and a simple rectangular region of the screen.  There's got to be a way to do a similar rectangular selection operation through code and achieve similar performance.


@Anonymous,

 

Whatever the procedure mentioned in the above statement. Please make sure that files are non confidential.

 

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 15 of 26

Anonymous
Not applicable

I have attached a screenshot below of the window select operation.  This is not a VBA function.  It's literally just a click and drag.  All I want to do is simulate this same functionality using code.

 

Inventor Drawing Window Select.png

Note:  The cursor is not visible because the Windows "Print Screen" function hides it.

0 Likes
Message 16 of 26

CadUser46
Collaborator
Collaborator

I googled, found this https://wellsr.com/vba/2015/excel/vba-mouse-move-and-mouse-click-macro/

 

Modified to this

'Declare mouse events
Public Declare PtrSafe Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
Public Declare PtrSafe Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4

Sub MoveMouse()

    'You need to set focus on the application
    SetCursorPos 1200, 500
    mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
    SetCursorPos 600, 800
    mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0

End Sub

Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro
0 Likes
Message 17 of 26

Anonymous
Not applicable

Thank you, I will give this a try.  I was hoping to avoid moving the mouse directly, but if this is the only option then this is what I'll have to use.

 

Is there a way to convert from screen coordinates to sheet coordinates?  I don't know where on the screen my drawing view will be, but I do know where on the sheet it is.

0 Likes
Message 18 of 26

CadUser46
Collaborator
Collaborator

Mate, i literally spent a few minutes googling this.  I imagine there is some way to read the position of the graphics window (via Inventor api) and/or convert this into a percentage of the screen resolution (Windows api).  I'm well outside the 'I've tried this before' range.  I simply have never needed to do this.

 

As a pure guess.

Use the application windows handle MainFrameHWND to set focus or something.

Zoom extents the sheet so you know where it's expected to be.

The api exposes thisapplication.height and .width, you could use these to estimate where you want to select.

If you use thisapplication.views.item(x).height/width it gives the values of the graphics window.  But not their position on the monitor.

 

I still have a feeling there must be a better way to achieve what you want but i really don't fully understand your description.


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro
0 Likes
Message 19 of 26

CadUser46
Collaborator
Collaborator

Are you sure you cant just read

thisapplication.activedocument.sheets.drawingviews.position.x/y, and height/width

then use that along with the same values for each symbol to tell if any of them are overlapping.  I'm right in thinking this is just like hit boxes in a game? 

 

Loop the dawing views. Make some kind of array/collection of the x coords that are occupied by views.  Then do the same for all the y coords. ie 10-45, 65-80, 95-150.

 

Then loop the symbols.  Calculate their four extents.  Check if each exists inside the sets X and Y (union of X and Y) then you know it overlaps.  Just a thought as it's easier to use what the api exposes, rather than force it to work how you're thinking.


Did you find this reply helpful ? If so please use the Accept as Solution or Kudos button below.

---------------------------------------------------------------------------------------------------------------------------
Inventor 2010 Certified Professional
Currently using 2023 Pro
0 Likes
Message 20 of 26

Anonymous
Not applicable

I really appreciate your willingness to help me out.  I know this is a weird use case and I'm not doing a very good job explaining myself.  Here is an example of a drawing view with some annotations that describe what I'm trying to detect.  Basically I want to know (through programming) "can I place an annotation here and not have it overlap something?".

Drawing Text Interference.PNG

 

You are correct, it's something like hitboxes in a game.  Unfortunately simple rectangular "hitboxes" won't suffice, I need more precise "collision detection".  The problem is that most of our pages are very tightly packed (these drawings are for part manuals) and I can't afford to only place annotations outside of the view's bounding box (which would be really easy).

 

Because of that, I can't generalize the shape of drawing views as rectangles because some of our geometry leaves plenty of white space inside the view bounding box that is available for annotations.  Here is a hand-drawn example:

20190507_115238.jpg

NOTE:  The black rectangles don't represent any actual object on a sheet, just an area that I'm interested in placing an annotation.

0 Likes