.NET

Reply
Valued Contributor
muthineni
Posts: 73
Registered: ‎10-10-2011
Message 1 of 12 (557 Views)
Accepted Solution

Get Modelspace objects

557 Views, 11 Replies
12-01-2011 04:10 AM
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.

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 -

*Expert Elite*
Alfred.NESWADBA
Posts: 9,574
Registered: ‎06-29-2007
Message 2 of 12 (552 Views)

Re: Get Modelspace objects

12-01-2011 04:41 AM 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
-------------------------------------------------------------------------
Valued Contributor
muthineni
Posts: 73
Registered: ‎10-10-2011
Message 3 of 12 (550 Views)

Re: Get Modelspace objects

12-01-2011 04:48 AM 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.
*Expert Elite*
Alfred.NESWADBA
Posts: 9,574
Registered: ‎06-29-2007
Message 4 of 12 (548 Views)

Re: Get Modelspace objects

12-01-2011 04:50 AM 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
-------------------------------------------------------------------------
Valued Contributor
muthineni
Posts: 73
Registered: ‎10-10-2011
Message 5 of 12 (546 Views)

Re: Get Modelspace objects

12-01-2011 05:03 AM 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.
Valued Contributor
muthineni
Posts: 73
Registered: ‎10-10-2011
Message 6 of 12 (540 Views)

Re: Get Modelspace objects

12-01-2011 05:14 AM 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.

*Expert Elite*
Alfred.NESWADBA
Posts: 9,574
Registered: ‎06-29-2007
Message 7 of 12 (534 Views)

Re: Get Modelspace objects

12-01-2011 05:38 AM 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
-------------------------------------------------------------------------
Valued Contributor
muthineni
Posts: 73
Registered: ‎10-10-2011
Message 8 of 12 (513 Views)

Re: Get Modelspace objects

12-01-2011 09:31 PM 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.
*Expert Elite*
Alfred.NESWADBA
Posts: 9,574
Registered: ‎06-29-2007
Message 9 of 12 (506 Views)

Re: Get Modelspace objects

12-01-2011 11:07 PM 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
-------------------------------------------------------------------------
Valued Contributor
muthineni
Posts: 73
Registered: ‎10-10-2011
Message 10 of 12 (501 Views)

Re: Get Modelspace objects

12-02-2011 12:02 AM 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

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.