.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Why my Polyline does not show up?

1 REPLY 1
Reply
Message 1 of 2
wesbird
321 Views, 1 Reply

Why my Polyline does not show up?

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
Windows 10 64 bit, AutoCAD (ACA, Map) 2023
1 REPLY 1
Message 2 of 2
Kamala
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.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost