Click Button to draw a Polyline

Click Button to draw a Polyline

Anonymous
Not applicable
529 Views
4 Replies
Message 1 of 5

Click Button to draw a Polyline

Anonymous
Not applicable
I'm coding to get all coordinates from a datagridview in palette, but it has some problems:

If use sub to add polyline individual, it work correct, but when insert this sub into click even of button (button1_click) it can't work

How to do it?

thanks
0 Likes
530 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable

<quote>

"If use sub to add polyline individual, it work correct"

</quote>

 

How do you use the "sub", do you mean a subroutine
decorated with CommandMethod attribute?

 

What does "it can't work" mean? Whay exception your
code runs into?

 

Since you mentioned palette, is the said button on
the palette? If so, it is very likely you need to lock the Document.

 

Finally, vague description and not showing relevant
code does not help to find answer to your question.

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
I'm
coding to get all coordinates from a datagridview in palette, but it has some
problems:

If use sub to add polyline individual, it work correct, but
when insert this sub into click even of button (button1_click) it can't
work

How to do it?

thanks
0 Likes
Message 3 of 5

Anonymous
Not applicable


I build a class like this:



public class LithorLayer

{

public void DisplayInModelSpace(Editor Ed)

{

Polyline PlLay;

BlockTableRecord btr;

BlockTable bt;

Database Db;

Transaction trans;

Db = HostApplicationServices.WorkingDatabase;

trans = Db.TransactionManager.StartTransaction();

try

{

PlLay = new Polyline(5);

PlLay.AddVertexAt(0, new Point2d(0, 0), 0, -1, -1);

PlLay.AddVertexAt(1, new Point2d(1, 0), 0, -1, -1);

PlLay.AddVertexAt(2, new Point2d(1, 1), 0, -1, -1);

PlLay.AddVertexAt(3, new Point2d(0, 1), 0, -1, -1);

PlLay.AddVertexAt(4, new Point2d(0, 0), 0, -1, -1);

bt = (BlockTable)trans.GetObject(Db.BlockTableId, OpenMode.ForRead);

btr = (BlockTableRecord)trans.GetObject(Db.CurrentSpaceId, OpenMode.ForWrite);

btr.AppendEntity(PlLay);

trans.AddNewlyCreatedDBObject(PlLay, true);

trans.Commit();

}

catch

{

Ed.WriteMessage("Error\n");

}

finally

{

trans.Dispose();

}

}

}



and use it in command like this:



public class tdcCommands

{

public tdcCommands()

{

}

[CommandMethod("Pll")]

static public void Pll()

{

Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

LithorLayer Lop;

Lop = new LithorLayer();

Lop.DisplayInModelSpace(ed);

}

}



It work!!!

But when I use like:



private void button1_Click(object sender, EventArgs e)

{

Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

LithorLayer Lop;

Lop = new LithorLayer();

Lop.DisplayInModelSpace(ed);

}



with button1 is a command button on a palette.

it doesn't work

Help me!! Thanks so much





Edited by: ledungtdc on Feb 4, 2009 9:52 AM
0 Likes
Message 4 of 5

Anonymous
Not applicable

As I expected in previous reply, you need to lock
the document when modifying drawing database from modeless form/palette. If you
do it from a modal form, it should work as you do from command line. Do not ask
me why: that is how autodesk implements the drawing database transaction.

 

So, you need to change your code similar to
this:

 

//Note, the argument doc, you can get it prior to
calling this method

//from Application.DocumentManager.MdiDocument, as
long as

//it is a Document object. That is, you need to get
a hold on a drawing

//document, to which you intent to modify its
database, and lock it

//during the database change, then release the
lock.

public void MakeChangeToDrawingDatabase(Document
doc)

{

    using (DocumentLock
lk=doc.LockDocument())

    {

        //Add/Modify
your polyline, or whatever entities here

        //you can use
try...catch...finally, if you prefer, as your code shown

        //but "using"
might be more suitable to wrap a transaction

        using
(Transaction
tran=doc.Database.TransactionManager.StartTransaction())

       
{

       
    //code to add/modify

       
}

    }

}

 

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">


I build a class like this:



public class
LithorLayer

{

public void DisplayInModelSpace(Editor
Ed)

{

Polyline PlLay;

BlockTableRecord
btr;

BlockTable bt;

Database Db;

Transaction
trans;

Db = HostApplicationServices.WorkingDatabase;

trans =
Db.TransactionManager.StartTransaction();

try

{

PlLay =
new Polyline(5);

PlLay.AddVertexAt(0, new Point2d(0, 0), 0, -1,
-1);

PlLay.AddVertexAt(1, new Point2d(1, 0), 0, -1,
-1);

PlLay.AddVertexAt(2, new Point2d(1, 1), 0, -1,
-1);

PlLay.AddVertexAt(3, new Point2d(0, 1), 0, -1,
-1);

PlLay.AddVertexAt(4, new Point2d(0, 0), 0, -1, -1);

bt =
(BlockTable)trans.GetObject(Db.BlockTableId, OpenMode.ForRead);

btr =
(BlockTableRecord)trans.GetObject(Db.CurrentSpaceId,
OpenMode.ForWrite);

btr.AppendEntity(PlLay);

trans.AddNewlyCreatedDBObject(PlLay,
true);

trans.Commit();

}

catch

{

Ed.WriteMessage("Error\n");

}

finally

{

trans.Dispose();

}

}

}



and
use it in command like this:



public class
tdcCommands

{

public
tdcCommands()

{

}

[CommandMethod("Pll")]

static
public void Pll()

{

Editor ed =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

LithorLayer
Lop;

Lop = new
LithorLayer();

Lop.DisplayInModelSpace(ed);

}

}



It
work!!!

But when I use like:



private void
button1_Click(object sender, EventArgs e)

{

Editor ed =
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

LithorLayer
Lop;

Lop = new
LithorLayer();

Lop.DisplayInModelSpace(ed);

}



with
button1 is a command button on a palette.

it doesn't work

Help
me!! Thanks so much





Edited by: ledungtdc on Feb 4, 2009
9:52 AM
0 Likes
Message 5 of 5

Anonymous
Not applicable
Thanks so much! It works
0 Likes