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 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.
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.
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?
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
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).
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:
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.