Problem creating layer

Problem creating layer

Anonymous
Not applicable
1,051 Views
9 Replies
Message 1 of 10

Problem creating layer

Anonymous
Not applicable

Hello guys I really need some help, I'm trying to create a new layer seting its name through a windows form, but my code always break, for example, this code works:

 

 LayerTable lT = tr.GetObject(bd.LayerTableId, OpenMode.ForRead) as LayerTable;
                    string sLayerName = "StringTest";

                    if (lT.Has(sLayerName) == false) 
                    {
                        LayerTableRecord lTR = new LayerTableRecord();
                        lTR.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 10);
                        lTR.Name = sLayerName;
                        
                        lT.UpgradeOpen();

                        lT.Add(lTR);
                        tr.AddNewlyCreatedDBObject(lTR, true);

                    }

but when I do some thing like:

 

 
string name = textBox.Text;

LayerTable lT = tr.GetObject(bd.LayerTableId, OpenMode.ForRead) as LayerTable; string sLayerName = name; if (lT.Has(sLayerName) == false) // Verify if this layer is already created { LayerTableRecord lTR = new LayerTableRecord(); lTR.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 10); lTR.Name = sLayerName; lT.UpgradeOpen(); lT.Add(lTR); tr.AddNewlyCreatedDBObject(lTR, true); }

 Making the user set the name for the future layer, my code breaks, help me guys I have no idea how to solve this.

0 Likes
1,052 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable
And to be a little bit more expecific I'm getting an exception when I'm trying work with that name variable.
0 Likes
Message 3 of 10

kerry_w_brown
Advisor
Advisor

You'll need to be more explicit about the location and message of the exception.

 

Is the layer code works stand alone why do you think it is the cause of the error ??

Isn't it more likely that the Form or the assignment to the name variable is the issue ?

 

How are you calling the Form ?

 

Is the command method that instantiates the form is static or not ?

 

What is the value of name if you add a WriteLine (or an Alert) in place of the layer making code call ?

 

Regards,

 

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes
Message 4 of 10

BKSpurgeon
Collaborator
Collaborator

please highlight the exact line that the code is breaking and what the exception is.

 

I can't see a reason why it should fail.

 

Perhaps check what the name string is set to, by adding a break point and checking the watch window to see what the name variable is actually equal to.

Message 5 of 10

Anonymous
Not applicable

 

Thanks guys, so here it is, my code is something like:

 

 public string roomName = "";

public void setCurrentRoomName(string CurrentName)
{
roomName = CurrrentName;
}


[CommandMethod("Walls")] public void setWalls() { if (roomName == "") { Form8 formName = new Form8(this); formAmbientName.Show(); } else { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Editor ed = doc.Editor; Database bd = doc.Database; Transaction tr = bd.TransactionManager.StartTransaction(); ObjectIdCollection ids = new ObjectIdCollection(); using (tr) { BlockTable bT; bT = tr.GetObject(bd.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord btR; btR = tr.GetObject(bT[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; LayerTable lT = tr.GetObject(bd.LayerTableId, OpenMode.ForRead) as LayerTable; string sLayerName = roomName; if (lT.Has(sLayerName) == false) { LayerTableRecord lTR = new LayerTableRecord(); lTR.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, 10); lTR.Name = sLayerName; lT.UpgradeOpen(); lT.Add(lTR); tr.AddNewlyCreatedDBObject(lTR, true); } PromptPointResult ppR; PromptPointOptions ppO = new PromptPointOptions(""); Point2dCollection p2dC = new Point2dCollection(); ppO.Message = "Specify the first point of the wall: "; ppR = doc.Editor.GetPoint(ppO); p2dC.Add(new Point2d(ppR.Value.X, ppR.Value.Y)); Point2d pontoInicial = new Point2d(ppR.Value.X, ppR.Value.Y); if (ppR.Status == PromptStatus.Cancel) return; int auxStartWhile = 1; while (pontoInicial != new Point2d(ppR.Value.X + auxStartWhile, ppR.Value.Y)) { ppO.Message = "Specify next point of the wall: "; ppO.UseBasePoint = true; ppO.BasePoint = ppR.Value; ppR = doc.Editor.GetPoint(ppO); p2dC.Add(new Point2d(ppR.Value.X, ppR.Value.Y)); auxStartWhile = 0; } using (SpecialBuildingWall pL = new SpecialBuildingWall()) { for (int i = 0; i < (p2dC.Count); i++) // that -1 exclude the point (0,0) that is created when click esc { pL.AddVertexAt(i, p2dC[i], 0, 0, 0); // add points to the polyline } pL.Layer = sLayerName; btR.AppendEntity(pL); tr.AddNewlyCreatedDBObject(pL, true); SpecialBuildingWall novoElementoEspecial = new SpecialBuildingWall(wallHeightParam, pL.Length, pL.Handle.ToString()); listadeRooms.Add(novoElementoEspecial); listadeRoomsAtualizada.Clear(); for (long i = 0; i < bd.Handseed.Value; i++) { ObjectId id = ObjectId.Null; bd.TryGetObjectId(new Handle(i), out id); if (id != ObjectId.Null && !id.IsErased) ids.Add(id); } foreach (ObjectId id in ids) { DBObject obj = tr.GetObject(id, OpenMode.ForRead, false) as DBObject; if (obj.GetRXClass().DxfName == "LWPOLYLINE") { object valueOfHandle_ = null; valueOfHandle_ = obj.GetType().GetProperty("Handle").GetValue(obj, null); for (int i = 0; i < listadelinhasespeciais.Count; i++) { ed.WriteMessage(valueOfHandle_.ToString()); if (listadeRooms[i].handle_.ToString() == valueOfHandle_.ToString()) { auxiliarIndex = i; listadeRoomsAtualizada.Add(listadeRooms[i]); //listadelinhasespeciaisAtualizada.Add(listadelinhasespeciais[i]); } } } } Form9 roomComponentsForm = new Form9(roomName, this, novoAmbiente); roomComponentsForm.Show(); } tr.Commit(); } } }

 

 

And the code of the form8 is:

 

 

 private void button1_Click(object sender, EventArgs e)
        {            
                instanciaDoMyCommands.setCurrentRoomName(textBox.Text);               
               Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("Walls ", true, false, true);
                this.Close();          
        }

 

The exception message apears after the form8 desapears.

 

 

Error Message

 

 

 

0 Likes
Message 6 of 10

SENL1362
Advisor
Advisor
Did you Lock the document before altering the Document/Database
Message 7 of 10

kerry_w_brown
Advisor
Advisor

pericles-terto,

 

How about you help others to help you.

Could you attach a zipped solution containing the bare minimun required to demonstrate your issue. 

 

That is the only way someone can test your code without guessing about the stuff you don't show.

 

 

Regards,

 


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
Message 8 of 10

Anonymous
Not applicable

I finally found the solution for my problem guys, thanks for you all that tried helping me. really, thank you very much.

0 Likes
Message 9 of 10

Jeff_M
Consultant
Consultant
How about posting what you found to be the solution so others may benefit in the future?
Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 10 of 10

Anonymous
Not applicable

I'm sorry, I wasn't at my computer to send u my solution before, well, my program was verifying the already created layer names two times

if (lT.Has(sLayerName) == false)

one into the same commandmethod as standard, and other into another external method called from the form that the user types the new layer name, I removed the verification of the other method and then the program worked fine.

0 Likes