Window Select/Region is Empty on Inventor Drawing with VBA

Window Select/Region is Empty on Inventor Drawing with VBA

Anonymous
Not applicable
2,999 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
3,000 Views
25 Replies
Replies (25)
Message 21 of 26

CadUser46
Collaborator
Collaborator

OK, now it's clear to me.  I did wonder if boxes would be too coarse.

Out of curiosity, how would you know programmatically what text has to go on what views and where?  Or are you trying to check the validity of text that has already been placed by an operator?

 

If it's the latter, and i'm not sure if any of this is possible, what if you

- Loop the text/symbol objects

- Use the mouse_downleft to collect a selectset of just the extent of each symbol based on it's position values.

- If the select set has more than just the text/symbol object (eg selectset.count > 1) then

- take some action like nudge the symbol, then retry.


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 22 of 26

Anonymous
Not applicable

I'm going to check a predefined position (which will depend on what the annotation is) and if that position is not free I will check a few locations around that point until I find a free location.

 

I think I'm going to have to do exactly what you suggest.

 

I was hoping to avoid moving the cursor through code because A) it seems sketchy and like there's a better way to do it and B) I would like the user to be able to minimize inventor while this macro runs and do other things (it might take a couple minutes to position everything in extreme cases).

 

However it seems like that's the only option so that's what I'll do.  Thank you so much for your help!

0 Likes
Message 23 of 26

CadUser46
Collaborator
Collaborator

Just had another idea, could be madness setting in.

If you know the size of each text/symbol, you could divide each drawing view up into blocks of some size and iterate them with repeated selectsets looking for blank areas.  Add in a quality check for each time it guesses.

May not be optimal but i cant think of any other ways to detect white space.

 

Also, just thinking about something i seen in this video, which i watched at random a while back but was quite impressed by.

https://www.youtube.com/watch?v=fURH8z3hb6Y

He uses code to grab the colour of each pixel.  I have no idea how you would translate this but what if you could just look for anything that is not the sheet colour?  And no, i have no idea how that would be achieved.


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 24 of 26

Anonymous
Not applicable

Thumbs up for Code Bullet!  I watch each of his videos within a few minutes of him uploading them 😉

 

Another thing I still have to address with this is converting between screen and sheet coordinates.  I think I'll do what you suggest earlier and do a zoom extents first and then get the viewport size and do my own calculations...  Annoying, but doable.

 

Also, I had to tweak the code you suggested earlier to actually get the window select to work properly (for some reason it wasn't selecting line segments, just whole objects like views and stuff).

 

 

'At the top of my module
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_MOVE = &H1
Public Const MOUSEEVENTF_LEFTDOWN = &H2
Public Const MOUSEEVENTF_LEFTUP = &H4

'Wherever in my module
Sub MoveMouse()

    'You need to set focus on the application
    SetCursorPos 1200, 600
    ThisApplication.ActiveDocument.Activate
    mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
    mouse_event MOUSEEVENTF_MOVE, -200, -200, 0, 0
    DoEvents
    mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0

End Sub

MOUSEEVENTF_MOVE has to be used AND DoEvents has to be called in order for the full window select functionality apparently.  The move event uses relative screen coordinates instead of absolute coordinates, so that's something to be aware of.  There is an event flag that can be set in order to make it use absolute coords, but I have yet to implement that.  (The documentation on the mouse_event function)

 

I also added the "ThisApplication.ActiveDocument.Activate" because I was having issues with the viewport not getting focus after I clicked the button to run the macro.

 

0 Likes
Message 25 of 26

Maxim-CADman77
Advisor
Advisor

I wonder whether you managed to cope with the task?

Please vote for Inventor-Idea Text Search within Option Names

0 Likes
Message 26 of 26

gerrardhickson
Collaborator
Collaborator

I realise this is an old post, but I stumbled upon while trying to figure out how to perform a window select (or equivalent) with VBA.

The last part of your question is about finding the white space on a drawing so you can position your notes. This I can help with. Firstly - as far as I know you can't do it in Inventor VBA, but I have done it in Python before - so you could write a python function and return a value if needed. This problem uses Kadane's algorithm -  which looks for the 'largest sum contiguous subarray'.  The method, if you wanted to explore this, would be to take a screenshot of the window, then using OpenCV to perform the search. There's plenty of sample code available if you google, so check it out.

So - did you find a solution to performing a window selection in Inventor?

0 Likes