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

Get Modelspace objects

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
muthineni
2381 Views, 11 Replies

Get Modelspace objects

I have to change all the text objects in the drawing model space based on the text object layer. if object is in "a" layer, then color should be changed to Blue and "b" layer objects should be changed to Green color. Can anyone help me on this issue? Thank you.
11 REPLIES 11
Message 2 of 12
Alfred.NESWADBA
in reply to: muthineni

Hi,

 

what have you tried yet? Or do you look for someone writing code for you (SCNR)?

There are so much examples in the www, in the >>>ObjectARX-kit<<<, on >>>Kean Walmsley's site<<<, ... that go through BlockTableRecords, that changes objects-properties, ...

 

If you have tried something ==> show the code-snippet and we can then work on that.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 12
muthineni
in reply to: Alfred.NESWADBA

I have one more doubt. Don't know whether i can ask this doubt or not. After pasting the code, all the lines are getting displayed as a continuous text. How to paste it as a snippet. Because of this i am unable to paste the code.
Message 4 of 12
Alfred.NESWADBA
in reply to: muthineni

Hi,

 

in the windows where you write your answers to the forum you have a toolbar above the textwindow and there is one icon for inserting code (left of the word-icon).

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 5 of 12
muthineni
in reply to: Alfred.NESWADBA

I am not getting any king of icons on the page. I am not able to attach the image also.
Message 6 of 12
muthineni
in reply to: muthineni

I have used the below code,

 

 <CommandMethod("DTC")> _
   Public Sub routine1()
        Dim ENT As DBText
        '' Get the current document and database
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database

        '' Start a transaction
        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
            '' Open the Block table 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)
            MsgCount = 0
            '' Step through the Block table record
            For Each acObj As Object In acBlkTblRec
                ENT = acObj
                If ENT.Layer = "HTSV_P1_BEWR_VOLL_X01" Then
                   ent.colorindex=4
                ElseIf ENT.Layer = "HTSV_P1_BEWR_RAST_X02" Then
                   e.clorindex=5
                End If
            Next
        End Using
    End Sub
                End If
            Next
        End Using
    End Sub

I am getting error new "ENT=acobj" line.

Message 7 of 12
Alfred.NESWADBA
in reply to: muthineni

Hi,

 

This part can't work:

For Each acObj As Object In acBlkTblRec
   ENT = acObj
   If ENT.Layer = "HTSV_P1_BEWR_VOLL_X01" Then
     ent.colorindex=4
   ElseIf ENT.Layer = "HTSV_P1_BEWR_RAST_X02" Then
     e.clorindex=5
   End If
Next

 ...because the BlockTableRecord is a list of ObjectIDs, not a list of Entities, so you have to change it to:

For each tObjID as ObjectID in acBlkTblRec
   'check if this ObjectID is valid within the database (that's a standardcheck for me
   if (tObjID.isValid) andalso (not tObjID.isErased) then
      'then you wanted to check that is of type Text
      if tObjID.ObjectClass.DxfName.ToUpper like "*TEXT" then
         'ok, it's an object of TEXT od MTEXT
         'get the object
         dim tEnt as Entity = ctype(acTrans.GetObject(tObjID,openmode.forread),Entity)
         select case tEnt.Layer.toupper
            case "HTSV_P1_BEWR_VOLL_X01"
               call changeColor(acTrans,tEnt,4)
            case "HTSV_P1_BEWR_RAST_X02"
               call changeColor(acTrans,tEnt,5)
         end select
      end if
   end if
Next


... and the new function for changing the colorindex
private sub changeColorIndex(byref TrAct as transaction, byref Ent as Entity, byval newColorIndex as integer)
  if Ent.ColorIndex <> newColorIndex then
     if not ent.isWriteEnabled then Ent = ctype(acTrans.GetObject(tObjID,openmode.forwrite),Entity)
     ent.ColorIndex = newColorIndex
  end if
end sub

 be careful, just written in Mozilla, not tested.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 8 of 12
muthineni
in reply to: Alfred.NESWADBA

Hi alfred, thank you so much for the reply, but i am getting error near, If tObjID.ObjectClass.DxfName.ToUpper Like "*TEXT" Then line, it is saying that "ObjectClass" is not a member of 'Autodesk.AutoCAD.DatabaseServices.ObjectId. I am using AutoCAD 2007. I have added the acdbmgd.dll and acmgd.dll reference from 2010, then it is showing error as below, System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.MissingMethodException: Method not found: 'Autodesk.AutoCAD.Runtime.RXClass Autodesk.AutoCAD.DatabaseServices.ObjectId.get_ObjectClass()'. Could you please help me out? Thank you.
Message 9 of 12
Alfred.NESWADBA
in reply to: muthineni

Hi,

 

>> I am using AutoCAD 2007

Sorry, within 2007 there was no access to the ObjectClass like I showed in the code. So remove the If-line that checks the objecttype (plus the according End If) and do the check after tEnt is assigned.

 

>> I am using AutoCAD 2007. I have added the acdbmgd.dll and acmgd.dll reference from 2010

Sorry, why are you doing that? If you are running AutoCAD 2007 you have to take the *mgd.dll from AutoCAD 2007.

 

And back to topic, for getting all textobjects in the modelspace I would prefere the way using a Editor-Selectionset, let AutoCAD do the selection for all text objects instead of scanning through all entities. Of course that will only work if you have the drawing opened in the editor.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 10 of 12
muthineni
in reply to: Alfred.NESWADBA

Now it is working fine with the below code,

  <CommandMethod("DTC")> _
   Public Sub routine1()
        Dim ENT As DBText
        '' Get the current document and database
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim acCurDb As Database = acDoc.Database

        '' Start a transaction
        Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
            '' Open the Block table 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)
            MsgCount = 0

            '' Step through the Block table record
            For Each tObjID As ObjectId In acBlkTblRec
                'check if this ObjectID is valid within the database (that's a standardcheck for me
                If (tObjID.IsValid) AndAlso (Not tObjID.IsErased) Then

                    Dim tEnt As Entity = CType(acTrans.GetObject(tObjID, OpenMode.ForWrite), Entity)
                    If TypeOf tEnt Is DBText Then
                        ENT = tEnt
                       'Remaining code
                    End If
                   
                End If
            Next

            acTrans.Commit()
            acTrans.Dispose()

            MsgBox("Completed Successfully.", MsgBoxStyle.Information, "HTI Tools")
        End Using

    End Sub

 Thank you so much

Message 11 of 12
Hallex
in reply to: muthineni

Just a hint:

If you've used

Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
acTrans.Commit()
 ''acTrans.Dispose()
End Using

You have to avoid

acTrans.Dispose()

 

because of this consruction is means that this object

will be disposed automatically inside this code block

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 12 of 12
muthineni
in reply to: Hallex

Thank you so much for the hint

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost