.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Select Block based on Block Handle ID

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
conveyor1
1275 Views, 7 Replies

Select Block based on Block Handle ID

Morning,

 

I am checking to see if anyone knows how to select an autocad block based on it's handle directly vs. looping thru all the blocks in the Block table record?

 

Currently, in the code below, it loops thru all the blocks in the drawing until it finds the specific one I need.  i was wondering if anyone had a more direct path.

 

Handles are stored in an excel file created in a previous extraction.

 

Please let me know.

 

        Using mytrans As Transaction = mytransMan.TransactionManager.StartTransaction()
            Dim mybt As BlockTable = mytrans.GetObject(mytransMan.BlockTableId, OpenMode.ForRead)
            Dim modelBtr As BlockTableRecord = mytrans.GetObject(mybt(BlockTableRecord.ModelSpace), OpenMode.ForRead)
            Dim BlockCounter As Integer = 0
            Do While CurRow <> LastRow + 1
                Block_Updated = False
                For Each myObjID As ObjectId In modelBtr
                    If myObjID.ObjectClass.Name = "AcDbBlockReference" Then
                        myBlockRef = myObjID.GetObject(DatabaseServices.OpenMode.ForRead)
                        If myBlockRef.Handle.Value = GetExcelValue(myXLWS.Cells(CurRow, 18)) Then
                            If GetExcelValue(myXLWS.Cells(CurRow, 2)) <> Class1_Obj.ReadAttributeValue(myObjID, "CID") Then
                                Class1_Obj.Setattributes(myObjID, "CID", GetExcelValue(myXLWS.Cells(CurRow, 2)))
                            End If
                            Block_Updated = True
                        End If
                    End If
                Next
                If Block_Updated = False Then
                    MsgBox("CID " & GetExcelValue(myXLWS.Cells(CurRow, 2)) & " is not in Drawing.  BOM Will Be Updated")
                    BOM_Regen = True
                End If
                CurRow = CurRow + 1
                BlockCounter = BlockCounter + 1
                ProgressBar1.Value = BlockCounter
            Loop
Tags (2)
7 REPLIES 7
Message 2 of 8
_gile
in reply to: conveyor1

Hi,

 

You can use the Database.GetObjectId() method, or better, the undocumented one: TryGetObjectId().

 

If the handle is stored as an hexadecimal string

    if (db.TryGetObjectId(new Handle(Convert.ToInt64(someHandleString, 16)), out ObjectId id))
        // do what you need with id
    else
        // the handle does not exist

 If the handle is sored as a long (Int64)

    if (db.TryGetObjectId(new Handle(someHandleValue), out ObjectId id))
        // do what you need with id
    else
        // the handle does not exist


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 8
_gile
in reply to: _gile

It should be something like this with VB:

 

Dim handle As Handle = New Handle(485) ' or: New Handle(Convert.ToInt64("1E5", 16))
Dim id As ObjectId = ObjectId.Null
If db.TryGetObjectId(handle, id) Then
    ' do what you need with id
Else
    ' the handle does not exist
End if


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 8
conveyor1
in reply to: _gile

Just check, but what is "db" again?

 

Just trying to figure out where to put it in the code.

Message 5 of 8


@conveyor1 wrote:

Just check, but what is "db" again?

 

Just trying to figure out where to put it in the code.


It is look like in your code db is equal mytransMan

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 6 of 8
_gile
in reply to: conveyor1

As said upper, GetobjectId() and TryGetObjectId() methods are instance methodes of the Database class, so 'db' should be the Database the handle belongs to.

As @Alexander.Rivilis said, 'mytransMan in your code.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 8
_gile
in reply to: _gile

Maybe this little example is more self explanatory.

 

C#

        [CommandMethod("TESTHANDLE")]
        public static void TestHandle()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            var pr = ed.GetString("\nHandle: ");
            if (pr.Status == PromptStatus.OK)
            {
                if (long.TryParse(pr.StringResult, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out long result))
                {
                    var handle = new Handle(Convert.ToInt64(pr.StringResult, 16));
                    if (db.TryGetObjectId(handle, out ObjectId id))
                        Application.ShowAlertDialog(id.ObjectClass.Name);
                    else
                        Application.ShowAlertDialog("Invalid Handle");
                }
                else
                {
                    Application.ShowAlertDialog("Invalid hexadecimal format");
                }
            }
        }

VB

    <CommandMethod("TESTHANDLE")>
    Public Shared Sub TestHandle()

        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim db As Database = doc.Database
        Dim ed As Editor = doc.Editor

        Dim promptResult As PromptResult = ed.GetString(vbCr & "Enter Handle: ")
        If promptResult.Status = PromptStatus.OK Then
            Dim handleNumber As Long
            If Long.TryParse(promptResult.StringResult, NumberStyles.HexNumber, CultureInfo.InvariantCulture, handleNumber) Then
                Dim handle As Handle = New Handle(handleNumber)
                Dim id As ObjectId
                If db.TryGetObjectId(handle, id) Then
                    Application.ShowAlertDialog(id.ObjectClass.Name)
                Else
                    Application.ShowAlertDialog("Invalid Handle")
                End If
            Else
                Application.ShowAlertDialog("Invalid hexadecimal number")
            End If
        End If
    End Sub


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 8
conveyor1
in reply to: _gile

That worked.  Thank you.

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

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report