Author Topic: Trying to change block color  (Read 2565 times)

0 Members and 1 Guest are viewing this topic.

Proctor

  • Guest
Trying to change block color
« on: May 29, 2008, 03:51:29 PM »
Hello:
I'm creating new block and then trying to change it's color before inserting into modelspace. After it's created, it's propterties show that the color is correct; however, it does not appear to be correct by looking at it in modelspace.

Code: [Select]

    <CommandMethod("addcircle")> _
    Public Sub AddNewBTRB()
        Dim myTransMan As DatabaseServices.TransactionManager
        Dim myTrans As DatabaseServices.Transaction
        Dim myDwg As Document
        Dim myBT As BlockTable
        Dim myBTR As BlockTableRecord

        myDwg = Application.DocumentManager.MdiActiveDocument
        myTransMan = myDwg.TransactionManager
        myTrans = myTransMan.StartTransaction

        'Open the database for Write
        myBT = myDwg.Database.BlockTableId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)

        'Add a new BlockTableRecord
        myBTR = New BlockTableRecord
        myBTR.Name = "CircleBlock"

        'Draw to the Block
        myBTR.AppendEntity(New DatabaseServices.Circle( _
            New Geometry.Point3d(0, 0, 0), New Geometry.Vector3d(0, 0, 1), 1.5))

        myBT.Add(myBTR)

        'Commit the Transaction
        myTrans.AddNewlyCreatedDBObject(myBTR, True)
        myTrans.Commit()

        'Dispose of the Transaction Objects
        myTrans.Dispose()
        myTransMan.Dispose()
    End Sub



    <CommandMethod("insertcircle")> _
       Public Sub InsertBlockB()
        Dim myTransMan As DatabaseServices.TransactionManager
        Dim myTrans As DatabaseServices.Transaction
        Dim myDwg As Document
        Dim myBT As BlockTable
        Dim myBTR As BlockTableRecord

        myDwg = Application.DocumentManager.MdiActiveDocument
        myTransMan = myDwg.TransactionManager
        myTrans = myTransMan.StartTransaction

        'Open the database for Write
        myBT = myDwg.Database.BlockTableId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
        myBTR = myBT(BlockTableRecord.ModelSpace).GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)

        'Insert the Block

        Dim myBlockRef As New DatabaseServices.BlockReference( _
            New Geometry.Point3d(2, 3, 0), myBT("CircleBlock"))
        myBlockRef.Rotation = 45 * Math.PI / 180
[color=red]        myBlockRef.Color = Colors.Color.FromRgb(200, 0, 200)[/color]
        myBTR.AppendEntity(myBlockRef)

        'Commit the Transaction
        myTrans.AddNewlyCreatedDBObject(myBlockRef, True)
        myTrans.Commit()

        'Dispose of the Transaction Objects
        myTrans.Dispose()
        myTransMan.Dispose()
    End Sub


I don't know what I'm doing wrong.
Thank you for your help,
Proctor

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Trying to change block color
« Reply #1 on: May 29, 2008, 03:56:07 PM »

Perhaps the author of the code could help you.

... abd an acknowledgement wouldn't go astray either.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Proctor

  • Guest
Re: Trying to change block color
« Reply #2 on: May 29, 2008, 03:58:40 PM »
Hi Kerry:

Gee Wiz...you're right...

...code compliments of Jerry Winters.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Trying to change block color
« Reply #3 on: May 29, 2008, 04:07:22 PM »
AFAIK, all items written to a block are treated as having been placed on the current layer with the current color. Thus the circle in the block would likely not be set to layer 0 or the color to byblock, causing the circle to inherit the color layer it was created on. You should set the layer property of the nested objects to 0 and/or set the color property of the objects to byblock, then it should inherit the color properties of the block.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Glenn R

  • Guest
Re: Trying to change block color
« Reply #4 on: May 29, 2008, 04:14:33 PM »
Look up SetDatabaseDefaults() - you should generally use this when adding entities

Glenn R

  • Guest
Re: Trying to change block color
« Reply #5 on: May 29, 2008, 04:16:43 PM »
Also if you're changing the colour of the Block reference itself, you will only notice the changes if the entities inside the block are defined with colour etc. as bylayer, or more importantly, byblock.

Glenn R

  • Guest
Re: Trying to change block color
« Reply #6 on: May 29, 2008, 04:17:20 PM »
...it goes without saying that this will only work for entities on layer 0.....

Proctor

  • Guest
Re: Trying to change block color
« Reply #7 on: May 29, 2008, 05:16:39 PM »
Thanks so much for your help....got it to work!!!  Because I'm so new at this...sometimes I'll use the snippets from Jerry's book first; after which, I customize it from there.  I'm going need to create a new layer before adding my blocks..so I also added that into the code below and now it works just like I need it.

Setting the color of the object used to create the block before creating the block is what I needed to do.

Thanks to all for your help!!!
Proctor


Code: [Select]

  <CommandMethod("addcircle")> _
       Public Sub AddNewBTRB()

        Dim myDwg As Document
        Dim myBT As BlockTable
        Dim myBTR As BlockTableRecord
        Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument()
        Dim ed As Editor = doc.Editor

        'add new layer here:
        Dim curdb As Database = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database
        Using docLock As DocumentLock = ed.Document.LockDocument()
            Using trans As Transaction = ed.Document.TransactionManager.StartTransaction()

                Dim lt As LayerTable = CType(trans.GetObject(curdb.LayerTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite), LayerTable)

                Dim x As New LayerTableRecord

                myLayerName = "6 PER FT"

                With x
                    .Name = myLayerName
                    .IsOff = False
                    .IsFrozen = False
                    .IsLocked = False

                    lt.Add(x)
                    trans.AddNewlyCreatedDBObject(x, True)
                End With

                myDwg = Application.DocumentManager.MdiActiveDocument

                'Open the database for Write
                myBT = myDwg.Database.BlockTableId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)

                'Add a new BlockTableRecord
                myBTR = New BlockTableRecord
                myBTR.Name = "CircleBlock"

                Dim CenterPoint As New Geometry.Point3d(0, 0, 0)
                Dim myCircle As New DatabaseServices.Circle(CenterPoint, New Geometry.Vector3d(0, 0, 1), 1.5)
                myCircle.Color = Colors.Color.FromRgb(200, 0, 200)

                'Draw to the Block
                myBTR.AppendEntity(myCircle)
                myBT.Add(myBTR)

                'Commit the Transaction
                trans.AddNewlyCreatedDBObject(myBTR, True)
                trans.Commit()

            End Using
        End Using

    End Sub


    <CommandMethod("insertcircle")> _
       Public Sub InsertBlockB()
        Dim myTransMan As DatabaseServices.TransactionManager
        Dim myTrans As DatabaseServices.Transaction
        Dim myDwg As Document
        Dim myBT As BlockTable
        Dim myBTR As BlockTableRecord
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument()
        Dim ed As Editor = doc.Editor


        myDwg = Application.DocumentManager.MdiActiveDocument
        myTransMan = myDwg.TransactionManager
        myTrans = myTransMan.StartTransaction

        'Open the database for Write
        myBT = myDwg.Database.BlockTableId.GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
        myBTR = myBT(BlockTableRecord.ModelSpace).GetObject(Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)

        'Insert the Block
        Dim myBlockRef As New DatabaseServices.BlockReference( _
            New Geometry.Point3d(2, 3, 0), myBT("CircleBlock"))
        myBlockRef.Rotation = 45 * Math.PI / 180
        myBlockRef.Layer = myLayerName

        myBTR.AppendEntity(myBlockRef)

        'Commit the Transaction
        myTrans.AddNewlyCreatedDBObject(myBlockRef, True)
        myTrans.Commit()

        'Dispose of the Transaction Objects
        myTrans.Dispose()
        myTransMan.Dispose()
    End Sub