VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Drawing a selection-like polygon window

5 REPLIES 5
Reply
Message 1 of 6
nicolas.roussel.fr
1776 Views, 5 Replies

Drawing a selection-like polygon window

Hi, I'm working on a VBA macro that allows me to pick up points defining a shape (shape properties are then exported to another software). I'd like to be able to display a kind of polygon selection window such as the built-in area command. Is there a way to achieve that ? Thanks Regards.
5 REPLIES 5
Message 2 of 6
RICVBA
in reply to: nicolas.roussel.fr

not sure what you're after. If you could post some example it would help

 

maybe SelectByPoligon method of the SelectionSet object could be of any help

its online AutoCAD Active X and VBA Reference, says the following

 

SelectByPolygon Method

Selects objects within a fence and adds them to the selection set.

 

Signature

object.SelectByPolygon Mode, PointsList, FilterType, FilterData

 

Object

SelectionSet
The object this method applies to.

 

Mode

AcSelect enum; input-only

acSelectionSetFence

acSelectionSetWindowPolygon

acSelectionSetCrossingPolygon

 

PointsList

Variant (array of doubles); input-only
An array of 3D WCS coordinates specifying the selection fence.

 

FilterType

Integer; input-only; optional
A DXF group code specifying the type of filter to use.

 

FilterData

Variant; input-only; optional
The value to filter on.

 

Remarks

The following selection modes are available:

Fence

Selects all objects crossing a selection fence. The fence is defined by coordinates in Point1.

 

WindowPolygon

Selects objects within a polygon defined by Point1.

 

CrossingPolygon

Selects objects within and crossing an area defined by a polygon. Use Point1 to define the coordinates of the polygon. AutoCAD will close the last vector of the polygon. A polygon definition cannot cross itself.

This method supports the filtering mechanism.

 

and comes with the following example, too

Sub Example_Select()
    ' This example adds members to a selection set, first by crossing and
    ' then by filtering for circles.
    
    ' Create the selection set
    Dim ssetObj As AcadSelectionSet
    Set ssetObj = ThisDrawing.SelectionSets.Add("SSET")
    
    
    ' Add all object to the selection set that lie within a crossing of (28,17,0) and
    ' (-3.3, -3.6,0) 
    Dim mode As Integer
    Dim corner1(0 To 2) As Double
    Dim corner2(0 To 2) As Double
    
    mode = acSelectionSetCrossing
    corner1(0) = 28: corner1(1) = 17: corner1(2) = 0
    corner2(0) = -3.3: corner2(1) = -3.6: corner2(2) = 0
    ssetObj.Select mode, corner1, corner2
    
    ' Add all the Circles to the selection set that lie within the crossing of (28,17,0) and
    ' (-3.3, -3.6,0) by filtering from the current drawing
    Dim gpCode(0) As Integer
    Dim dataValue(0) As Variant
    gpCode(0) = 0
    dataValue(0) = "Circle"
    
    Dim groupCode As Variant, dataCode As Variant
    groupCode = gpCode
    dataCode = dataValue
    
    ssetObj.Select mode, corner1, corner2, groupCode, dataCode
    
End Sub
Message 3 of 6
nicolas.roussel.fr
in reply to: RICVBA

Thanks for your answer.

 

To elaborate my request, I don't need any actual selection mechanism, I'm just looking to display the selection polygon.

Example : If you run the built in "area" command, it lets you pick up some points to define the shape of which you want to measure the area. When picking points, it displays the polgon defined by those points which looks like a selection polygon but doesn't actually selects anything.

 

Regards.

Message 4 of 6
RICVBA
in reply to: nicolas.roussel.fr

You can have your VBA routine make the user select as many points as you like and draw lines between them. Is that what you need?
If you post your code it would be much simpler helping you
Message 5 of 6

With VBA, you are basically out of luck.

 

You can do it fairly easy with .NET API (or ObjectARX C++, of course).

 

In case you do know a bit .NET API, here is an example

 

http://drive-cad-with-code.blogspot.ca/2011/01/mimicking-autocads-area-command-with_20.html

 

However, if you really need to mimic the action with VBA, you might try to actually draw/erase/redraw lines connecting the points with each point user picks. At the end, simple erase all the lines. It is not elegent, but you can try.

 

Again, for this kind of tempotatry graphics, using ObjectARX/.NET API is the solution to go. Better moving to .NET API than being stuck in VBA.

Message 6 of 6
RICVBA
in reply to: norman.yuan

Norman's piece of advice and links are very fine

 

in VBA the quickiest (and roughest, indeed) way would be add a Form with a button whose click event could be

 

Private Sub CbSelectionLikePolygon_Click()
Dim returnPnt As Variant, returnPnt2 As Variant
Dim iPoints As Integer
Dim Points() As Double
Dim AcadPoly As AcadLWPolyline

Me.Hide

On Error Resume Next

returnPnt = ThisDrawing.Utility.GetPoint(, "select first point: ")
iPoints = 1
If Not (IsNull(returnPnt)) Then
    ReDim Points(0 To 2 * iPoints - 1)
    Points(0) = returnPnt(0): Points(1) = returnPnt(1)
    
    Do
        returnPnt2 = ThisDrawing.Utility.GetPoint(returnPnt, "Next Point (<Esc> to Finish): ")
        If Err Then
            returnPnt2 = Null
        Else
            Call AddPoint(returnPnt2, Points, iPoints, AcadPoly)
            returnPnt = returnPnt2
        End If
        
    Loop While Not IsNull(returnPnt2)
    
End If

On Error GoTo 0

Me.Show
End Sub

where the button is supposed to be called "CbSelectionLikePolygon"

Tha "AddPoint" sub should take care of updating Points() array, delete previous LWPoly and redraw the new one

like follows

Sub AddPoint(Pnt As Variant, Points() As Double, iPoints As Integer, AcadPoly As AcadLWPolyline)
    If iPoints > 1 Then AcadPoly.Delete
    iPoints = iPoints + 1
    ReDim Preserve Points(0 To 2 * iPoints - 1)
    Points(2 * iPoints - 2) = Pnt(0): Points(2 * iPoints - 1) = Pnt(1)
    Set AcadPoly = ThisDrawing.ModelSpace.AddLightWeightPolyline(Points)
    AcadPoly.Closed = True
End Sub

to get nearer to Norman's .NET code functioning it is also possible to add a hatch to theLWPoly

but still you'd be missing the constant updating of LWPoly and hatch while moving around the mouse for the next input point

I guess it could be achieved if it would be possible to catch mouse movements via some Autocad events handler and consequently update LWPoly and its hatch. but this is something I never struggled with. And not sure about its performances however...

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

Post to forums  

Autodesk Design & Make Report

”Boost