Jump to content

Trying to color lines - autocad 2009 .net api


Proctor

Recommended Posts

Hello: I want to modify the color of some lines I created earlier in my subroutine, but it's not working.

 

Earlier in the sub, I call DrawLines to draw the lines (working fine):

 

Public Shared Function DrawLine(ByVal pStart, ByVal pEnd, ByVal sColor) As Line
           Dim myTransMan As Autodesk.AutoCAD.DatabaseServices.TransactionManager
           Dim myTrans As Autodesk.AutoCAD.DatabaseServices.Transaction
           Dim myDWG As Autodesk.AutoCAD.ApplicationServices.Document
           Dim myBT As Autodesk.AutoCAD.DatabaseServices.BlockTable
           Dim myBTR As Autodesk.AutoCAD.DatabaseServices.BlockTableRecord
           'Get the active document and begin a Transaction
           myDWG = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
           myTransMan = myDWG.TransactionManager
           myTrans = myTransMan.StartTransaction
           'Open the BlockTable for Read
           myBT = myDWG.Database.BlockTableId.GetObject( _
               Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
           myBTR = myBT(Autodesk.AutoCAD.DatabaseServices.BlockTableRecord.ModelSpace).GetObject( _
               Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
           'Draw the line
           Dim myLine As New Autodesk.AutoCAD.DatabaseServices.Line(pStart, pEnd)
           'was this line already just drawn? 
           Select Case sColor
               Case "YellowGreen"
                   myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.YellowGreen)
               Case "Pink"
                   myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Pink)
               Case "Purple"
                   myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Purple)
               Case "Blue"
                   myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Blue)
               Case "Coral"
                   myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Coral)
                   'small lines used just to see if the merge is correct - temp lines
               Case "Gold"
                   myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Gold)
               Case "Red"
                   myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Red)
           End Select
           myLine.Layer = 0
           myBTR.AppendEntity(myLine)
           myTrans.AddNewlyCreatedDBObject(myLine, True)
           DrawLine = myLine
           'Commit the Transaction
           myTrans.Commit()
           myTrans.Dispose()
           myTransMan.Dispose()
       End Function

 

Later, I want to modify the color of the lines I drew, so i try to do something link this:

 

For m As Integer = 0 To myListOfEdges.Count - 1
                               Edge = myListOfEdges.Item(m)
                               Debug.Print("edge: " & Edge.sTypeEdge.ToString())
                               Debug.Print("line len: " & Edge.dTotalLengthOfLines.ToString())
                               If Not IsNothing(Edge.lstLinesInEdge) And Edge.sTypeEdge.ToString() = "secondary" Then
                                   For j As Integer = 0 To Edge.lstLinesInEdge.Count - 1
                                       Dim oLine As Line = Edge.lstLinesInEdge(j)
                                       Debug.Print("line len: " & oLine.Length.ToString())

'main line that attempts to change color of line:      

[color=red]oLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Green)[/color]
                                   Next
                               End If
                           Next
tr.commit

 

After the subroutine finishes and I can see the drawing in cad, the color of the lines didn't change. I try doing a regen to refresh the screen, but the color of the lines still haven't changed.

 

I'm wondering what am i missing?

 

Thanks,

Proctor

Link to comment
Share on other sites

Hi StevJ: Thanks for your reply. I went to try that; however, the intellisense for the FromColor() method pops up and has me pick out

a Drawing.Color from it's list so I believe that parts ok.

Thanks again for your help,

Proctor

Link to comment
Share on other sites

Is the Line contained within Edge.lstLinesInEdge(j) already database resident. If so then it would typically need to be OpenMode.ForWrite before any of it’s properties can be changed.

 

I suppose it is not impossible to have a collection of “Open” database resident objects, but that sound a bit risky. Does the routine’s objective require dealing with entities in that fashion?

Link to comment
Share on other sites

I agree with stevj if your going to play around with colours its a lot easier to use the numbers then you don't have to strart remembering two different colour systems AcRed v's 1 AcYellow v's 2 also when you start using more than color 9 its easier to remember 20 32 50 140 (we plot in b&w plus colour) red orange yellow lightblue

Link to comment
Share on other sites

Thank you all for your help. I hope you don't mind but, I think I'd like to back the truck up a bit...because I'm now thinking that perhaps I'm confused in regards to what exactly needs to happen when creating lines.

 

1) My code starts asks the user to select a poly and then it locks the document and starts a transaction:

 

If Not IsNothing(myPSR.Value) Then
                   Using docLock As DocumentLock = myEd.Document.LockDocument()
                       Using tr As Transaction = myDB.TransactionManager.StartTransaction()

 

2) It then loops through each of the poly vertecies and creates lines:

For i = 0 To pPoly.NumberOfVertices - 1
   If i > 0 Then
         myLine = New Line(myPt, pPoly.GetPoint3dAt(i))
   End if
next

 

I add these lines into a collection to be used later;

At some point in my sub, I then want to display one of the lines in my collection.

 

Originally, I was uncertain how to display the line....so I called my DrawLine routine. it takes in the line's start and end points as a parameter and is basically creating the line. but, isn't the line already created? This was the only way i was able to get the line to show up and now that I'm thinking about it....i don't think this is right. I think there must be some how to display the line created originally in my code (using code under #2).

 

Thanks for your help....

Proctor

Link to comment
Share on other sites

ok..i just read that in order to draw the line ...when it's created, you need to add it to the block table record. so now, as i'm looping through and creating each line, I'm adding it to the block table record:

 

[size=2][color=#0000ff][size=2][color=#0000ff]For[/color][/size][/color][/size][size=2] i = 0 [/size][size=2][color=#0000ff][size=2][color=#0000ff]To[/color][/size][/color][/size][size=2] pPoly.NumberOfVertices - 1[/size][size=2][color=#0000ff][size=2][/size][/color][/size][size=2]myLine = [/size][size=2][color=#0000ff][size=2][color=#0000ff]New[/color][/size][/color][/size][size=2] Line(myPt, pPoly.GetPoint3dAt(i))
[/size][size=2]CreateLine(myLine, [/size][size=2][color=#800000][size=2][color=#800000]"Red"[/color][/size][/color][/size][size=2])[/size]

 

i modified my DrawLine so that i can pass in the line just created and add it to the btr:

 

[size=2][color=#0000ff][size=2][color=#0000ff]Public[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Shared[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Function[/color][/size][/color][/size][size=2] CreateLine([/size][size=2][color=#0000ff][size=2][color=#0000ff]ByVal[/color][/size][/color][/size][size=2] myLine, [/size][size=2][color=#0000ff][size=2][color=#0000ff]ByVal[/color][/size][/color][/size][size=2] sColor) [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Line
[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] myTransMan [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.DatabaseServices.TransactionManager
[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] myTrans [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.DatabaseServices.Transaction
[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] myDWG [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.ApplicationServices.Document
[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] myBT [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.DatabaseServices.BlockTable
[/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] myBTR [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.DatabaseServices.BlockTableRecord
[/size][size=2][color=#008000][size=2][color=#008000]'Get the active document and begin a Transaction
[/color][/size][/color][/size][size=2]myDWG = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
myTransMan = myDWG.TransactionManager
myTrans = myTransMan.StartTransaction
[/size][size=2][color=#008000][size=2][color=#008000]'Open the BlockTable for Read
[/color][/size][/color][/size][size=2]myBT = myDWG.Database.BlockTableId.GetObject( _
Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
myBTR = myBT(Autodesk.AutoCAD.DatabaseServices.BlockTableRecord.ModelSpace).GetObject( _
Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)
[/size][size=2][color=#008000][size=2][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Select[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2] sColor
[/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2][color=#800000][size=2][color=#800000]"YellowGreen"
[/color][/size][/color][/size][size=2]myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.YellowGreen)
[/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2][color=#800000][size=2][color=#800000]"Pink"
[/color][/size][/color][/size][size=2]myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Pink)
[/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2][color=#800000][size=2][color=#800000]"Purple"
[/color][/size][/color][/size][size=2]myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Purple)
[/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2][color=#800000][size=2][color=#800000]"Blue"
[/color][/size][/color][/size][size=2]myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Blue)
[/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2][color=#800000][size=2][color=#800000]"Coral"
[/color][/size][/color][/size][size=2]myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Coral)
[/size][size=2][color=#008000][size=2][color=#008000]'small lines used just to see if the merge is correct - temp lines
[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2][color=#800000][size=2][color=#800000]"Gold"
[/color][/size][/color][/size][size=2]myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Gold)
[/size][size=2][color=#0000ff][size=2][color=#0000ff]Case[/color][/size][/color][/size][size=2][color=#800000][size=2][color=#800000]"Red"
[/color][/size][/color][/size][size=2]myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Red)
[/size][size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Select
[/color][/size][/color][/size][size=2]myLine.Layer = 0
myBTR.AppendEntity(myLine)
myTrans.AddNewlyCreatedDBObject(myLine, [/size][size=2][color=#0000ff][size=2][color=#0000ff]True[/color][/size][/color][/size][size=2])
CreateLine = myLine
[/size][size=2][color=#008000][size=2][color=#008000]'Commit the Transaction
[/color][/size][/color][/size][size=2]myTrans.Commit()
myTrans.Dispose()
myTransMan.Dispose()
[/size][size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][/color][/size][size=2][color=#0000ff][size=2][color=#0000ff]Function
[/color][/size][/color][/size]

 

now, I'm going to try to change the line's color...I'll get back to you shortly...I just wanted to let you know where I'm at now.

 

thanks,

Proctor

Link to comment
Share on other sites

Well..that's what the issue was..bad code. now that i'm creating the line and adding it to the btr, i'm able to change the color using:

 

colListOfLines(t).Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Blue)

 

thanks again for helping me.

Proctor

Link to comment
Share on other sites

Proctor,

a couple of things ..

Have you read the AutoCAD .NET Developer's Guide

http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html

 

Have a look at Use Transactions with the Transaction Manager

 

and

 

Consider making better use of namespaces instead of doing so much typing

ie if you have used

Imports Autodesk.AutoCAD.DatabaseServices

OR for C#

using Autodesk.AutoCAD.DatabaseServices

 

then this

Autodesk.AutoCAD.DatabaseServices.BlockTableRecord.ModelSpace

can become

BlockTableRecord.ModelSpace

 

at the end of the day, the code is easier to read and thats half the battle when starting.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...