Proctor 发表于 2022-7-6 11:00:05

Trying to color lines - autoca

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:      oLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Green)                                 Next                               End If                           Nexttr.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

StevJ 发表于 2022-7-6 11:09:13

You might have to use color numbers instead of color names.
chroma.dwg

Proctor 发表于 2022-7-6 11:20:37

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

SEANT 发表于 2022-7-6 11:24:47

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?

BIGAL 发表于 2022-7-6 11:31:54

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

Proctor 发表于 2022-7-6 11:39:22

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 ifnext
 
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

Proctor 发表于 2022-7-6 11:49:27

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:
 

For i = 0 To pPoly.NumberOfVertices - 1myLine = New Line(myPt, pPoly.GetPoint3dAt(i))CreateLine(myLine, "Red")
 
i modified my DrawLine so that i can pass in the line just created and add it to the btr:
 

PublicSharedFunction CreateLine(ByVal myLine, ByVal sColor) As LineDim myTransMan As Autodesk.AutoCAD.DatabaseServices.TransactionManagerDim myTrans As Autodesk.AutoCAD.DatabaseServices.TransactionDim myDWG As Autodesk.AutoCAD.ApplicationServices.DocumentDim myBT As Autodesk.AutoCAD.DatabaseServices.BlockTableDim myBTR As Autodesk.AutoCAD.DatabaseServices.BlockTableRecord'Get the active document and begin a TransactionmyDWG = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocumentmyTransMan = myDWG.TransactionManagermyTrans = myTransMan.StartTransaction'Open the BlockTable for ReadmyBT = myDWG.Database.BlockTableId.GetObject( _Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)myBTR = myBT(Autodesk.AutoCAD.DatabaseServices.BlockTableRecord.ModelSpace).GetObject( _Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite)SelectCase sColorCase"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 linesCase"Gold"myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Gold)Case"Red"myLine.Color = Autodesk.AutoCAD.Colors.Color.FromColor(Drawing.Color.Red)EndSelectmyLine.Layer = 0myBTR.AppendEntity(myLine)myTrans.AddNewlyCreatedDBObject(myLine, True)CreateLine = myLine'Commit the TransactionmyTrans.Commit()myTrans.Dispose()myTransMan.Dispose()EndFunction
 
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

Proctor 发表于 2022-7-6 11:55:23

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

Kerry Brown 发表于 2022-7-6 12:00:35

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.
页: [1]
查看完整版本: Trying to color lines - autoca