Strange SelectionSets behaviuor

Strange SelectionSets behaviuor

Anonymous
Not applicable
1,001 Views
4 Replies
Message 1 of 5

Strange SelectionSets behaviuor

Anonymous
Not applicable

Hope someone can really help in this weird case. I also looked at forum tickets but without a solution for me.

 

Very simple code:

 

Point1 = ThisDrawing.Utility.GetPoint(, vbCr & "Specify first corner:")

Point2 = ThisDrawing.Utility.GetCorner(Point1, "Enter Other corner: ")

 

MsgBox "The point picked is" & Point1(0) & ", " & Point1(1) & "-" & Point1(2) & "-----" & Point2(0) & ", " & Point2(1) & "-" & Point2(2)

 

‘ the displayed cords are correct!

 

Dim dblPoints(0 To 3) As Double

dblPoints(0) = Point1(0)

dblPoints(1) = Point1(1)

dblPoints(2) = Point2(0)

dblPoints(3) = Point2(1)

Set AcadPolyline = ThisDrawing.ModelSpace.AddLightWeightPolyline(dblPoints)

 

‘ the line is correct on layout, it connects the two selected point!

 

Dim objSSet As AcadSelectionSet  

Set objSSet = ThisDrawing.SelectionSets.Add("mySel")

objSSet.Select acSelectionSetCrossing, Point1, Point2

 

objSSet.Highlight True

MsgBox ("SELECTION COUNT: " & ThisDrawing.SelectionSets("mySel").Count)

 

 

 

The final selection is wrong! It seems that the Select function has a sort of offset (big) with respect to my selected points.

 

Does someone know the explanation of this behaviour? Is there something that I need to set/verify in autocad options?

 

Thank you,

 

Giuseppe

0 Likes
Accepted solutions (1)
1,002 Views
4 Replies
Replies (4)
Message 2 of 5

Ed__Jobe
Mentor
Mentor

I cleaned up your code. Here are a few suggestions. Always post your code in a code window (use the </> button in the post menu). Always use Option Explicit at the top of your module, it reminds you to dimension your variables. In the lwp case, never dim a var with spelling the same as a class. My naming convention for variable names representing objects is to precede it with "o" for "object" and then a shortcut name for the class, i.e. "PL". Another method I use for more complex functions is a prefix for the type and a name representing it's use, i.e. "plWindowBoundary".  Don't manually add a selection set. Use a function to trap for errors. I added mine for you. In your last MsgBox function, I reused the variable rather than going to the selectionsets collection.

 

There are two possible reasons why Count was zero. First, the sub adds the poly to ModleSpace. If you run the sub from PaperSpace, it won't find the pl. Second, it depends on how you selected the points for the window/pl. The Select method specifically calls point1 and then point 2. Then it creates a crossing window by reordering the points. So if you created your pl by picking upper right first and then lower left as if you were creating a crossing window, it would not work. To be more robust and allow the user, you would need to test the picked points to figure out which one was upper right and then supply the points to the Select function in the proper order.

 

 

 

Option Explicit
Sub test1()
    Dim point1 As Variant
    Dim point2 As Variant
    point1 = ThisDrawing.Utility.GetPoint(, vbCr & "Specify first corner:")
    point2 = ThisDrawing.Utility.GetCorner(point1, "Enter Other corner: ")
    
    MsgBox "The point picked is" & point1(0) & ", " & point1(1) & "-" & point1(2) & "-----" & point2(0) & ", " & point2(1) & "-" & point2(2)
    
    ' the displayed cords are correct!
    Dim dblPoints(0 To 3) As Double
    dblPoints(0) = point1(0)
    dblPoints(1) = point1(1)
    dblPoints(2) = point2(0)
    dblPoints(3) = point2(1)
    
    Dim oPL As AcadLWPolyline
    Set oPL = ThisDrawing.ModelSpace.AddLightWeightPolyline(dblPoints)
    oPL.Layer = "0"
    oPL.Update
    
    ' the line is correct on layout, it connects the two selected point!
    
    Dim objSSet As AcadSelectionSet
    Set objSSet = AddSelectionSet("mysel")
    objSSet.Select acSelectionSetCrossing, point1, point2
    objSSet.Highlight True
    
    MsgBox ("SELECTION COUNT: " & objSSet.Count)
End Sub

Public Function AddSelectionSet(SetName As String) As AcadSelectionSet
' This routine does the error trapping neccessary for when you want to create a
' selectin set. It takes the proposed name and either adds it to the selectionsets
' collection or sets it.
    On Error Resume Next
    Set AddSelectionSet = ThisDrawing.SelectionSets.Add(SetName)
    If Err.Number <> 0 Then
        Set AddSelectionSet = ThisDrawing.SelectionSets.Item(SetName)
        AddSelectionSet.Clear
    End If
End Function

 

 

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 5

Anonymous
Not applicable

Hello Ed,

thank you for the useful suggestions 🙂, I can confirm you that I’m running the VBA script from a modelSpace and the selection order is correct. After further investigating the issue,it seems that there is a difference between kind of cords I see by moving the crosshair on the sheet and the ones I see through the msgBox in the script.

 

It’s seems that they belong to two different scales and while AddLightWeightPolyline takes coords of the VBA “scale” .Select acSelectionSetCrossing take coords of the autocad sheet scale type.

 

Is there a way to make them “compatible” ?

 

Giuseppe

0 Likes
Message 4 of 5

Ed__Jobe
Mentor
Mentor

The code I posted works fine for me. If it doesn't work for you there may be something weird with your file. Please post a sample dwg that has the problem.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 5 of 5

Anonymous
Not applicable
Accepted solution

Hello Ed,

I found a solution to the issue. I added these lines after the 

 

 Dim pointT1, pointT2 As Variant
  pointT1 = ThisDrawing.Utility.TranslateCoordinates(Point1, acWorld, acUCS, False)
  pointT2 = ThisDrawing.Utility.TranslateCoordinates(Point2, acWorld, acUCS, False)
  objSSet.Select acSelectionSetCrossing, pointT1, pointT2

 and it worked.   Not sure if this translation is necessary only in my case or if it is advised always...

 

BR,

 

Giuse[[e 

0 Likes