I am writing a function to create a layer with as many attributes as I need in when creating a layer. The problem I am running into when creating a color definition to the layer. What sort of thing do I pass to the function to change color as a color index number? using col dim?
The passing call would look like this...
createNewLayer("E-SCHDL-BRDR", 15, 0.4) 'I would hope to somehow pass an interger of the to the sub, but get an error when trying to do so, of course
Private Sub createNewLayer(ByVal NewLayerName As String, ByVal AColor As Autodesk.AutoCAD.Colors.Color, ByVal AWidth As Double) Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim docLock As DocumentLock = doc.LockDocument() Dim ed As Editor = doc.Editor Dim db As Database = doc.Database Dim tr As Transaction = db.TransactionManager.StartTransaction 'Dim tm As DBTransMan = db.TransactionManager 'Dim dba As Database = HostApplicationServices.WorkingDatabase Try Dim LT As LayerTable = tr.GetObject(db.LayerTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead, False) If LT.Has(NewLayerName) = False Then Dim LTRec As New LayerTableRecord() LTRec.Name = NewLayerName LTRec.Color = AColor LTRec.LineWeight = AWidth LT.UpgradeOpen() LT.Add(LTRec) tr.AddNewlyCreatedDBObject(LTRec, True) tr.Commit() End If Finally tr.Dispose() End Try End Sub
Solved! Go to Solution.
Solved by conormccartney3897. Go to Solution.
modify the passing call to this:
createNewLayer("E-SCHDL-BRDR", Color.FromColorIndex(ColorMethod.ByAci, 15), 0.4)
If you don't have them yet, download the ObjectARX documents. There's a help file in there which provides all of the properties, methods, and objects in the Managed .NET API. For layer, the Color property is shown as "public Autodesk.AutoCAD.Colors.Color Color" (at least for my version). That indicates the value type (in this case, an object of type "Color". Looking in the Autodesk.AutoCAD.Colors Namespace, it shows the Colors object along with *its* properties and methods. The example from the other poster creates a Color object using a provided ACI index.
It seems like a lot of work compared to other means, but that is the price of flexibility.
Yes I am still learning and why my questions or perhaps solutions are the way they are. There may be other beter methods out there, but I don't know them yet. The biggest problem I face is examples and trying to scrounge code where I can get it. If that code isn't complete, such as the context, it gets harder.
I am greatful of all the help I get here.
The preceeding code did not update in the drawing. Is this because I did not upgradeopen and then downgradeopen when finishing out of function calls? Can locks be performed on mdiactivedoc in general to process all processes that occur in my code?
Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument Dim docLock As DocumentLock = doc.LockDocument() ... doc.UpgradeDocOpen()
createNewLayer("EPS Testing", Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 15), 0.4)
doc.DowngradeDocOpen(False)
Firstly, it's nice to see you doing more programming, jboone. :beer:
Couple of things that jumped out to me in passing....
I'm not sure I understand why the DocumentLock is necessary; here's an example from Kean (C#) that does not include a DocumentLock, nor Session flag.
Also, you show the AWidth parameter as Double, but at least for me here in C# LayerTableRecord.LineWeight only accepts an Autodesk.AutoCAD.DatabaseServices.LineWeight enum as valid typed value.
Cheers
"How we think determines what we do, and what we do determines what we get."
Thanks box. I had been working on another project that took all my learning power and now that that is done, I am breaking out trying to expand my wares to new more power methods of getting my jobs done. Am still learning here, but hope to become better as I get more code under my belt. Example code is my biggest hurdle because that is where I can make things happen without asking so many questions but am happy to have such a great community of people willing to help.
I may call on you to help me if you can spare some time, with the many monuntious problems I will face in the near future, which looks bright. Hope we meet again in our usual forums sites.
I'm always happy to help; you know where to reach me.
The only advice I can offer with confidence, is to keep in the front of your mind the question "and then what" which will keep you honest about the code you write, and to also occassionally pause to consider what other API could be used given the task at hand (aka "the right tool for the job")... Learnign .NET API for the sake of learning has value in its own right, but often times I jump back down to LISP for its simplicity, and speed (in writing the code).
I've come a long way in a short period of time, thanks to the kindness of others, and I am happy to pass what little I know well, along to you for consideration.
Cheers
"How we think determines what we do, and what we do determines what we get."
I seem to have hit a glitch with this function and don't know how to handle it, if someone would be so kind to review it. I would also like to set the layer as it is created (or check if it exist, then set).
It seems that the function works but not like one would expect. The first time in the function ran nothing appeared to happen in the layer manager and a line I drew was on a previous layer. The second time I ran the function I could then see that layer and the line I drew was a layer I specified. When I did a purge the line, layer where showing bu the layer was deleted it told me. sending a sendcommand is the only way I know to set the layer. Not sure if this works either.
createLayer("E-ANNO-BRDR", Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByAci, 9), LineWeight.LineWeight000) doc.SendStringToExecute("-LAYER" & vbCr & "S" & vbCr & "E-SCHDL-BRDR" & vbCr & vbCr, True, False, False)
'draw a line here
The create layer function: not working properly and I need to set to that layer if it does or doesn't exist.
Private Sub createLayer(ByVal LayerName As String, ByVal AColor As Autodesk.AutoCAD.Colors.Color, ByVal AWidth As Autodesk.AutoCAD.DatabaseServices.LineWeight) Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor Dim db As Database = ed.Document.Database Dim trans As Transaction = db.TransactionManager.StartTransaction() Dim layer1 As LayerTableRecord Try Dim layerTable As LayerTable = trans.GetObject(db.LayerTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead) ' see if testLayer exists If layerTable.Has(LayerName) Then ' instantiate the LayerTableRecord layer1 = trans.GetObject(layerTable(LayerName), Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite) Else ' Upgrade the open of the layer table layerTable.UpgradeOpen() ' create new layer table record layer1 = New LayerTableRecord() layer1.Name = LayerName ' set the color layer1.Color = AColor 'Color.FromColorIndex(ColorMethod.ByAci, 1) layer1.LineWeight = AWidth ' add layer table record layerTable.Add(layer1) ' add to transaction trans.AddNewlyCreatedDBObject(layer1, True) End If trans.Commit() Catch ex As System.Exception ed.WriteMessage(ex.ToString()) Finally trans.Dispose() End Try End Sub
See my earlier post for the link to Kean's article on creating a layer... Yes, it's in C#, but you should still be able to read the objects, properties, and methods... Specifically the call to SymbolUtilityServices.ValidateSymbolName(), and the call to Database.Clayer Property
The only other (practical) alternative that I can think of to change the current layer, is a call to Application.SetSystemVariable().
Cheers
"How we think determines what we do, and what we do determines what we get."