find the total number of lines with an angle equal to a selected line

find the total number of lines with an angle equal to a selected line

mecoman6GF27
Advocate Advocate
486 Views
5 Replies
Message 1 of 6

find the total number of lines with an angle equal to a selected line

mecoman6GF27
Advocate
Advocate

Hello everybody.
In a drawing I want to find the total number of lines with an angle equal to a selected line.
I tried this way.

    Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click

        Dim Dwg_Doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim Dwg_Db As Database = Dwg_Doc.Database
        Dim Dwg_Editor As Editor = Dwg_Doc.Editor

        Dim Line As Line
        Dim Angle_Line As Double

        Dim Sel_Line As PromptEntityResult = Dwg_Editor.GetEntity("Select the line: ")

        Using Trans As Transaction = Dwg_Db.TransactionManager.StartTransaction()
            Line = Sel_Line.ObjectId.GetObject(OpenMode.ForRead)
            Angle_Line = Linea.Angle
        End Using

        Dim Elenco_Filtri() As TypedValue = New TypedValue() {
            New TypedValue(DxfCode.Start, "LINE"),
            New TypedValue(DxfCode.Operator, "="),
            New TypedValue(DxfCode.Angle, Angle_Line)
            }

        Dim Linee_Filtrate_Sel As SelectionFilter = New SelectionFilter(Elenco_Filtri)
        Dim Linee_Filtrate As PromptSelectionResult = Dwg_Editor.SelectAll(Linee_Filtrate_Sel)

        MsgBox("Number line: " & Linee_Filtrate.Value.Count.ToString)
    End Sub


This exception is thrown:
"System.NullReferenceException: Reference to an object not set to an object instance.
Autodesk.AutoCAD.EditorInput.PromptSelectionResult.Value.get returned Nothing. "
Where do you think I'm wrong?
Thanks a lot, bye.

0 Likes
487 Views
5 Replies
Replies (5)
Message 2 of 6

norman.yuan
Mentor
Mentor

When you get runtime exception, the first thing to do, if you cannot identify the cause of the error from the error message (but in your case the exception message is fairly obvious), is to step through the code in debugging, which would easily/quickly leads you to the offending line of code. At that point, you probably have a clear idea of what is wrong with that line of code. So, my question is have you done the basic debugging before asking question here?

 

The error you get is likely from this line:

 

Line = Sel_Line.ObjectId.GetObject(OpenMode.ForRead)

 

where you use Sel_Line of PromptEntityResult obtained from Editor.GetEntity(). 

 

The good practice of using PromptXXXXResult returned from Editor.GetXxxx() methods is ALWAYS test its Status property and proceed according to Status property's value, because when user does selecting, he/she could select entity/enter keyword/hit Esc to cancel, enter invalid input, select wrong entity... thus the reason to test Status. so the code should be like:

 

Dim SlL_Lline As PromptEntityResult = Dwg_Editor.GetEntity(...)

If Sel_Line.Status = PromptStatus.Ok Then

  '' Proceed, because an valid entity is selected

End If

 

However, with your requirement, you may want to make sure you only allow user to pick a Line, not anything else. So, you may want to use PromptEntityOptions with the Editor.GetEntity(). In the PromptEntityOptions, you add Line as allowed entity.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 6

mecoman6GF27
Advocate
Advocate

Hello norman.yuan.
I modified the code following your advice. Now I don't get any more exceptions but I still can't get the total number of lines with the angle equal to the selected line.
This is the code I wrote.

    Private Sub B_Filo_Esterno_Muro_Click(sender As Object, e As EventArgs) Handles B_Filo_Esterno_Muro.Click

        Dim Dwg_Doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim Dwg_Db As Database = Dwg_Doc.Database
        Dim Dwg_Editor As Editor = Dwg_Doc.Editor

        Dim Sel_Linea As PromptEntityOptions = New PromptEntityOptions(vbLf & "Seleziona una linea: ")
        Sel_Linea.SetRejectMessage(vbLf & "L'oggetto selezionato non è una linea!!!")
        Sel_Linea.AddAllowedClass(GetType(Line), True)

        Dim Sel_Linea_Result As PromptEntityResult = Dwg_Editor.GetEntity(Sel_Linea)

        If Sel_Linea_Result.Status = PromptStatus.OK Then
            Dim Linea_Angolo As Double

            Using Transazione As Transaction = Dwg_Db.TransactionManager.StartTransaction()
                Linea = Sel_Linea_Result.ObjectId.GetObject(OpenMode.ForRead)
                Linea_Angolo = Linea.Angle
            End Using

            Dim Linee_Elenco_Filtri() As TypedValue = New TypedValue() {
                            New TypedValue(DxfCode.Start, "LINE"),
                            New TypedValue(DxfCode.Operator, "="),
                            New TypedValue(DxfCode.Angle, Linea_Angolo)
            }

            Dim Linee_Filtrate_Sel As SelectionFilter = New SelectionFilter(Linee_Elenco_Filtri)
            Dim Linee_Filtrate As PromptSelectionResult = Dwg_Editor.GetSelection(Linee_Filtrate_Sel)

            If Linee_Filtrate.Status = PromptStatus.OK Then
                MsgBox("Numero linee: " & Linee_Filtrate.Value.Count.ToString)
            Else
                MsgBox("Nessuna linea")
            End If
        Else
            Me.Close()
        End If
    End Sub


Where do you think I'm wrong?
Thanks a lot, bye.

0 Likes
Message 4 of 6

norman.yuan
Mentor
Mentor

Not every Entity property can be filtered, using DxfCode, in SelectionFilter. In your case, the Line's Angle is not filter-able.

 

You would only apply filter to select all lines and then use your code to examine the line's Angle. Sometimes, you may want to choose how to gather a group of entities: using Editor.SelectXXXX() with filter, or simply loop through entities in current space with code. If your case, the latter often easier to code, where you do not have to build a filter to select and then loop through the selected lines for compare Angle, something like:

 

var lineIds=new List<ObjectId>();

using (var tran=TheDB.TransactionManager.StartTransaction())

{

  var model=...; //Open ModelSpace BlockTableRecord

  foreach (ObjectId id in model)

  {

    if (id.ObjectClass.DxfName!="LINE") continue;

    var line=(Line)tran.GetObject(id, OpenMode.ForRead);

    if (line.Angle=[theAngle]) lineIds.Add(id)

  }

  tran.Commit();

}

//Now you have a list of ObjectId of lines that have the same angle

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 6

mecoman6GF27
Advocate
Advocate

Hello , thanks a lot.

0 Likes
Message 6 of 6

Ed__Jobe
Mentor
Mentor

If you type @, you don't have to hyperlink to a user's profile. Plus, when you use @, it sends them an email of your post.

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