.NET

Reply
Distinguished Contributor
367 Posts
0 Kudos
Registered: ‎12-05-2003
Post 1 of 2

Why my Polyline does not show up?

113 Views, 1 Replies
09-30-2005 08:39 AM
Hi,
I tried to create a function to draw a Polyline (LWPolyline). I tried to create function in a Class as static function(DrawBox) and invoke it from another Class(DrawBox_Test). when I test it, the Polyline does not show up. If I save drawing and reopen it, it show up. if create a block with select all (I can not pick it from drawing because it is not there), the Polyline show up.
When I put all code to 1 Class(DBox_Test), it works.
Here is the code:
In AutoCADLIB.cs:
using System;
using System.Collections;
using System.Globalization;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;

namespace ClassLibrary
{
///
/// Summary description for AutoCADLIB.
///

public class AutoCADLIB
{
//
// Draw a rectangle box
public static Entity DrawBox(Point2d pt, double Width, double Height, BlockTableRecord btr)
{
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForWrite);
BlockTableRecord btr2 = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite);

Polyline plBox;
try
{
plBox = new Polyline(4);
plBox.Normal = Vector3d.ZAxis;
plBox.AddVertexAt (0, pt, 0.0,-1.0,-1.0);
plBox.AddVertexAt (1, new Point2d(pt.X+Width, pt.Y),0.0,-1.0,-1.0);
plBox.AddVertexAt (2, new Point2d(pt.X+Width,pt.Y+Height),0.0,-1.0,-1.0);
plBox.AddVertexAt (3, new Point2d(pt.X,pt.Y+Height),0.0,-1.0,-1.0);
plBox.Closed = true;

btr2.AppendEntity(plBox);
trans.AddNewlyCreatedDBObject(plBox,true);
trans.Commit();
// plBox.Close();
}
catch (System.Exception caught)
{
ed.WriteMessage(caught.ToString());
plBox = null;
}
finally
{
trans.Dispose();
}
return plBox;
}
}
}


in Class.cs
using System;
using System.Collections;
using System.Globalization;

using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;

[assembly: CommandClass(typeof(ClassLibrary.Test))]


namespace ClassLibrary
{
///
/// Summary description for RSClass.
///

public class Test
{
//
[CommandMethod("DrawBox_TEST")]
public static void DrawBox_Test()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
Transaction trans = db.TransactionManager.StartTransaction();

BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForWrite);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite);
PromptPointOptions prPtOpts = new PromptPointOptions("Please pick a point:");

PromptPointResult prPtRes = ed.GetPoint(prPtOpts);

if (prPtRes.Status != PromptStatus.OK )
return;

Point3d pt1 = prPtRes.Value;
Point2d pt = new Point2d(pt1.X, pt1.Y);

Entity ent = AutoCADLIB.DrawBox(pt,100,50,btr);
if (ent != null)
ed.WriteMessage("{0}",ent.GetType().Name);
else
ed.WriteMessage("No Box Drawed.");
}

[CommandMethod("DBox_Test")]
public static void DBox_Test()
{
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForWrite);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite);
PromptPointOptions prPtOpts = new PromptPointOptions("Please pick a point:");

PromptPointResult prPtRes = ed.GetPoint(prPtOpts);

if (prPtRes.Status != PromptStatus.OK )
return;

Point3d pt1 = prPtRes.Value;
Point2d pt = new Point2d(pt1.X, pt1.Y);

Polyline plBox;
try
{
plBox = new Polyline(4);
plBox.Normal = Vector3d.ZAxis;
plBox.AddVertexAt (0, pt, 0.0,-1.0,-1.0);
plBox.AddVertexAt (1, new Point2d(pt.X+100, pt.Y),0.0,-1.0,-1.0);
plBox.AddVertexAt (2, new Point2d(pt.X+100,pt.Y+50),0.0,-1.0,-1.0);
plBox.AddVertexAt (3, new Point2d(pt.X,pt.Y+50),0.0,-1.0,-1.0);
plBox.Closed = true;

btr.AppendEntity(plBox);
trans.AddNewlyCreatedDBObject(plBox,true);
trans.Commit();
// plBox.Close();
}
catch (System.Exception caught)
{
ed.WriteMessage(caught.ToString());
}
finally
{
trans.Dispose();
}

// if (plBox != null)
// ed.WriteMessage("{0}",plBox.GetType().Name);
// else
// ed.WriteMessage("No Box Drawed.");

}

}
}

What's wrong in my code?
Thank you and I really appreaciate your help



Wes
Vault Pro 2014, Windows 7 64 bit, AutoCAD (ACA, Map) 2014
Contributor
18 Posts
0 Kudos
Registered: ‎03-01-2006
Post 2 of 2

Re: Why my Polyline does not show up?

03-22-2006 05:46 AM in reply to: wesbird
hi,

You have have added only 3 vertices to the polyline.if u the last vertex i.e.vertext 4 to the start vertex u can create polyline.
i.e.
plBox.AddVertexAt (3, new Point2d(pt.X,pt.Y+Height),0.0,-1.0,-1.0);
plBox.Closed = true;
instead
plBox.AddVertexAt (3, new Point2d(pt.X,pt.Y+Height),0.0,-1.0,-1.0);
plBox.AddVertexAt (4, new Point2d(pt.X,pt.Y+Height),0.0,-1.0,-1.0);
will solve the issue.
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post