Author Topic: How to cast an object into another...  (Read 2384 times)

0 Members and 1 Guest are viewing this topic.

EDDemtec

  • Guest
How to cast an object into another...
« on: November 11, 2011, 11:33:46 AM »
Hi Guys !

I would like to learn how to cast an object into another object of a different type, if it's possible...

In my case, I want to cast an ObjectId in a AecWall and get length of the current wall...

Code: [Select]
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AEC.Interop.ArchBase

Public Class Class1
    <CommandMethod("Class1")> _
    Public Sub Class1()
        '' Get the current document and database, and start a transaction
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database

        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
            '' Open the Block table record for read
            Dim acBlkTbl As BlockTable
            acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

            '' Open the Block table record Model space for read
            Dim acBlkTblRec As BlockTableRecord
            acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForRead)

            Dim myWall As New AecWall

            '' Step through each object in Model space and
            '' display the type of object found
            For Each acObjId As ObjectId In acBlkTblRec
                If acObjId.ObjectClass().DxfName = "AEC_WALL" Then
                    'Get wall length here

                End If
            Next

            '' Dispose of the transaction
        End Using
    End Sub
End Class

Thanks in advance !

huiz

  • Swamp Rat
  • Posts: 917
  • Certified Prof C3D
Re: How to cast an object into another...
« Reply #1 on: November 11, 2011, 01:52:20 PM »
DirectCast for example.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

huiz

  • Swamp Rat
  • Posts: 917
  • Certified Prof C3D
Re: How to cast an object into another...
« Reply #2 on: November 11, 2011, 01:54:53 PM »
Dim ent As Entity = DirectCast(tr.GetObject(myObjectID, OpenMode.ForRead), Entity)

Otherwise you can have a look at TryCast.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

huiz

  • Swamp Rat
  • Posts: 917
  • Certified Prof C3D
Re: How to cast an object into another...
« Reply #3 on: November 11, 2011, 01:56:35 PM »
Btw, if you only look for one type of object, you can create a Selection with a certain object type as filter. It reduces the loop.
The conclusion is justified that the initialization of the development of critical subsystem optimizes the probability of success to the development of the technical behavior over a given period.

EDDemtec

  • Guest
Re: How to cast an object into another...
« Reply #4 on: November 11, 2011, 02:25:01 PM »
Btw, if you only look for one type of object, you can create a Selection with a certain object type as filter. It reduces the loop.
Thank you for all !

I found the code below, but I dont want to prompt for a selection, is it possible ?

Code: [Select]
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Public Class Class2
    <CommandMethod("FilterSelectionSet")> _
    Public Sub FilterSelectionSet()

      '' Get the current document editor
      Dim acDocEd As Editor = Application.DocumentManager.MdiActiveDocument.Editor

      '' Create a TypedValue array to define the filter criteria
      Dim acTypValAr(0) As TypedValue
      acTypValAr.SetValue(New TypedValue(DxfCode.Start, "AEC_WALL"), 0)

      '' Assign the filter criteria to a SelectionFilter object
      Dim acSelFtr As SelectionFilter = New SelectionFilter(acTypValAr)

      '' Request for objects to be selected in the drawing area
      Dim acSSPrompt As PromptSelectionResult
      acSSPrompt = acDocEd.GetSelection(acSelFtr)

      '' If the prompt status is OK, objects were selected
      If acSSPrompt.Status = PromptStatus.OK Then
          Dim acSSet As SelectionSet = acSSPrompt.Value
          Application.ShowAlertDialog("Number of objects selected: " & acSSet.Count.ToString())
      Else
          Application.ShowAlertDialog("Number of objects selected: 0")
      End If

    End Sub
End Class
« Last Edit: November 11, 2011, 02:45:09 PM by EDDemtec »

Jeff_M

  • King Gator
  • Posts: 4094
  • C3D user & customizer
Re: How to cast an object into another...
« Reply #5 on: November 11, 2011, 04:33:45 PM »
Look at using the acDocEd.SelectAll(acSelFtr) method instead of the GetSelection() method.

EDDemtec

  • Guest
Re: How to cast an object into another...
« Reply #6 on: November 14, 2011, 10:32:13 AM »
Look at using the acDocEd.SelectAll(acSelFtr) method instead of the GetSelection() method.
Thank you very much for your help !

As sample, the code below, select all walls in the drawing...

Code: [Select]
Public Class FilterSelectionAll

    'Define the command
    <CommandMethod("fsa")> _
    Public Sub fsa()

        '' Get the current document editor
        Dim acDocEd As Editor = Application.DocumentManager.MdiActiveDocument.Editor

        '' Create a TypedValue array to define the filter criteria
        Dim acTypValAr(0) As TypedValue
        acTypValAr.SetValue(New TypedValue(DxfCode.Start, "AEC_WALL"), 0)

        '' Assign the filter criteria to a SelectionFilter object
        Dim acSelFtr As SelectionFilter = New SelectionFilter(acTypValAr)

        '' Request for objects to be selected in the drawing area
        Dim acSSPrompt As PromptSelectionResult
        acSSPrompt = acDocEd.SelectAll(acSelFtr)

        '' If the prompt status is OK, objects were selected
        If acSSPrompt.Status = PromptStatus.OK Then
            Dim acSSet As SelectionSet = acSSPrompt.Value
            Application.ShowAlertDialog("Number of objects selected: " & acSSet.Count.ToString())
        Else
            Application.ShowAlertDialog("Number of objects selected: 0")
        End If

    End Sub

End Class