Solved! Go to Solution.
Solved by Alfred.NESWADBA. Go to Solution.
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 -
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 -
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.
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 -
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 -
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
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'~