I choose a center point of a circle but draw it's center in (0,0,0)

I choose a center point of a circle but draw it's center in (0,0,0)

almutaz_86
Advocate Advocate
1,303 Views
4 Replies
Message 1 of 5

I choose a center point of a circle but draw it's center in (0,0,0)

almutaz_86
Advocate
Advocate

Good evening .

 

trying to write a code , drawing a circle after picking the center point ,

I choose a center point of a circle but draw it's center in (0,0,0)

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

[assembly: ExtensionApplication(typeof(GetPointTest2.test2))]

namespace GetPointTest2
{
public class test2:IExtensionApplication
{
[CommandMethod("test2")]
public void t2()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database dbs = doc.Database;
Editor editr = doc.Editor;
Transaction trans = dbs.TransactionManager.StartTransaction();
ObjectId modspc = dbs.CurrentSpaceId;
BlockTableRecord blkrec = trans.GetObject(modspc, OpenMode.ForWrite) as BlockTableRecord;
try
{
PromptPointOptions ppo = new PromptPointOptions("\n Pick a point ");
ppo.AllowArbitraryInput = false;
ppo.AllowNone = true;
PromptPointResult ppr1 = editr.GetPoint(ppo);
if (ppr1.Status!=PromptStatus.OK)
{
return;
}
ppo.BasePoint = ppr1.Value;
ppo.UseBasePoint = true;
PromptPointResult ppr2 = editr.GetPoint(ppo);
if (ppr2.Status!=PromptStatus.OK)
{
return;
}
Point3d cntr = new Point3d(ppo.BasePoint.X, ppo.BasePoint.Y, ppo.BasePoint.Z);
Vector3d vect = new Vector3d();
double rad = 10;
Circle circle = new Circle(cntr, vect, rad);
blkrec.AppendEntity(circle);
trans.AddNewlyCreatedDBObject(circle, true);
}
catch (Autodesk.AutoCAD.Runtime.Exception excep)
{
Application.ShowAlertDialog("" + excep );
}
trans.Commit();
}
void IExtensionApplication.Initialize()
{

}
void IExtensionApplication.Terminate()
{

}
}
}

 

thanks 🙂

0 Likes
Accepted solutions (1)
1,304 Views
4 Replies
Replies (4)
Message 2 of 5

essam-salah
Advisor
Advisor

Hi @almutaz_86 

why you ask for 2 points 

try this

PromptPointOptions ppo = new PromptPointOptions("\n Pick a point ");
ppo.AllowArbitraryInput = false;
ppo.AllowNone = true;
PromptPointResult ppr1 = editr.GetPoint(ppo);
if (ppr1.Status!=PromptStatus.OK)
{
return;
}

Vector3d vect = new Vector3d();
double rad = 10;
Circle circle = new Circle(ppr1.Value, vect, rad);
0 Likes
Message 3 of 5

_gile
Consultant
Consultant
Accepted solution

Hi

There are many things in your code that are not very clean or even wrong:

  • you must dispose of the transaction (the simplest way is to wrap it in a using statement)
  • passing a new Vector3d() (i.e., a null vector (0, 0, 0)) to the Circle constructor is certainly the cause of your error
  • you should not commit the transaction outside of the try block
  • you should not write useless code (as for a second point)
  • you should use a separated class to implement IExtensionApplication if you need this (unused here)
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(GetPointTest2.Test2))]

namespace GetPointTest2
{
    public class Test2
    {
        [CommandMethod("TEST2")]
        public static void Test()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var ppr = ed.GetPoint("\n Pick a point ");
            if (ppr.Status != PromptStatus.OK)
                return;
            var center = ppr.Value;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var circle = new Circle(center, Vector3d.ZAxis, 10.0);
                circle.TransformBy(ed.CurrentUserCoordinateSystem);
                var space = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                space.AppendEntity(circle);
                tr.AddNewlyCreatedDBObject(circle, true);
                tr.Commit();
            }
        }
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 5

almutaz_86
Advocate
Advocate

Thank you @_gile for your important notes, there is some codeblocks I was think its necessary

I'm still in first step of .net api  of AutoCAD

thank you again .

0 Likes
Message 5 of 5

almutaz_86
Advocate
Advocate

Thanks @essam-salah 

I tried your way, But its not working. 

0 Likes