Jump to content

Recommended Posts

Posted

I need to create a circle where the user clicks - but i need the circle to appear around the mouse-pointer before he clicks. Almost like when you are using the CP command, i.e. he can see the circle before he clicks, so he can decide where to move it.

 

It has to be VB.net code unfortunately, as it is part of a larger program. Any help will be much appreciated.

Posted

Thanx SEANT, I think that is what I need, but it seems a bit complicated for my level of experience. I'm also working in 2d, so I think I need a simplified version. I will take another look later on when I have time.

Posted

Hi,

 

Here's a little sample for your task.

It's C#, but may be converting it to VB will be a good way tu undertand it.

 

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

namespace CircleJigSample
{
   public class CircleJigClass
   {
       class CircleJig : EntityJig
       {
           Circle m_circle;
           double m_rad;
           Point3d m_center;

           public CircleJig(Circle myCircle)
               : base(myCircle)
           {
               m_circle = myCircle;
               m_rad = m_circle.Radius;
               m_center = m_circle.Center;
           }
           protected override bool Update()
           {
               m_circle.Radius = m_rad;
               return true;
           }

           protected override SamplerStatus Sampler(JigPrompts prompts)
           {
               JigPromptDistanceOptions jpdo = new JigPromptDistanceOptions("\nSpecify the radius: ");
               jpdo.BasePoint = m_center;
               jpdo.UseBasePoint = true;
               jpdo.UserInputControls =
                   UserInputControls.NoNegativeResponseAccepted |
                   UserInputControls.NoZeroResponseAccepted;
               PromptDoubleResult pdr = prompts.AcquireDistance(jpdo);
               if (pdr.Status == PromptStatus.OK)
               {
                   if (pdr.Value == m_rad)
                       return SamplerStatus.NoChange;
                   else
                   {
                       m_rad = pdr.Value;
                       return SamplerStatus.OK;
                   }
               }
               return SamplerStatus.Cancel;
           }
       }

       [CommandMethod("Test")]
       public void Test()
       {
           Document doc = Application.DocumentManager.MdiActiveDocument;
           Database db = doc.Database;
           Editor ed = doc.Editor;
           PromptPointResult ppr = ed.GetPoint("\nSpecify the center: ");
           if (ppr.Status == PromptStatus.OK)
           {
               using (Transaction tr = db.TransactionManager.StartTransaction())
               {
                   Matrix3d ucs = ed.CurrentUserCoordinateSystem;
                   Vector3d ucsNormal = new Vector3d(0.0, 0.0, 1.0).TransformBy(ucs);
                   Point3d center = ppr.Value.TransformBy(ucs);
                   Circle myCircle = new Circle(center, ucsNormal, 1.0);

                   CircleJig jig = new CircleJig(myCircle);
                   PromptResult res = ed.Drag(jig);
                   if (res.Status == PromptStatus.OK)
                   {
                       BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                       btr.AppendEntity(myCircle);
                       tr.AddNewlyCreatedDBObject(myCircle, true);
                   }
                   tr.Commit();
               }
           }
       }
   }
}

Posted

Here’s my interpretation of Jozi68’s original request (C# notwithstanding).

 

Actually, I’m not sure this routine matches the request, but the process seemed intriguing so I went for it. :)

 

 

Edit:

I included a Tolerance setting to avoid the error Gile mentioned in the following post.

 

If this code example is used with a drawing of small unit scale, the tolerance can be modified from the current 0.001 Equal Vector/Equal Point to something more appropriate.

 

As always, use test code with caution.

 

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

[assembly: CommandClass(typeof(CircleJigSample.Commands))]

namespace CircleJigSample
{

   public class Commands
   {
       [CommandMethod("Test2")]
       public void Test()
       {
           Document doc = Application.DocumentManager.MdiActiveDocument;
           Database db = doc.Database;
           Editor ed = doc.Editor;
           Tolerance Tol = new Tolerance(.001, .001);
           Matrix3d ucs = ed.CurrentUserCoordinateSystem;
           Vector3d ucsNormal = ucs.CoordinateSystem3d.Zaxis;
           PromptPointOptions ppo = new PromptPointOptions("Select initial Point: ");
           PromptPointResult ppr = ed.GetPoint(ppo);
           CircleJig jig = new CircleJig(ppr.Value, ucsNormal, .01, Tol);
           PromptResult res = ed.Drag(jig);

           try
           {
               using (Transaction tr = db.TransactionManager.StartTransaction())
               {
                   BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                   if (res.Status == PromptStatus.OK)
                   {
                       Circle circ = jig.GetEntity() as Circle;
                       if (circ.Radius <= Tol.EqualVector)
                       {
                           circ.Dispose();
                           return;
                       }
                       circ.SetDatabaseDefaults();
                       btr.AppendEntity(circ);
                       tr.AddNewlyCreatedDBObject(circ, true);
                       CircPlaceJig pjig = new CircPlaceJig(circ, circ.Center, ucs);
                       res = ed.Drag(pjig);
                       if (res.Status == PromptStatus.OK)
                       {
                           Circle newcirc = pjig.GetEntity() as Circle;
                           newcirc.SetDatabaseDefaults();
                           btr.AppendEntity(newcirc);
                           tr.AddNewlyCreatedDBObject(newcirc, true);
                       }
                       circ.Erase();
                   }
                   tr.Commit();
               }
           }
           catch
           {
               ed.WriteMessage("\nError processing circle!");
           }

       }


       class CircleJig : EntityJig
       {
           double m_rad;
           Point3d m_initial;
           Point3d m_center;
           Point3d m_temp;
           Vector3d m_norm;
           Tolerance m_tol;

           public CircleJig(Point3d Initial, Vector3d ucsNormal, Double Rad, Tolerance Tol)
               : base(new Circle(Initial, ucsNormal, Rad))

           {

               m_rad = Rad;
               m_center = Initial;
               m_initial = Initial;
               m_temp = m_center;
               m_norm = ucsNormal;
               m_tol = Tol;
           }
           protected override bool Update()
           {
               ((Circle)base.Entity).Center = m_center;
               ((Circle)base.Entity).Radius = m_rad;
               return true;
           }

           protected override SamplerStatus Sampler(JigPrompts prompts)
           {
               JigPromptPointOptions jpdo = new JigPromptPointOptions("\nSpecify the radius: ");
               jpdo.BasePoint = m_initial;
               jpdo.UseBasePoint = true;
               jpdo.UserInputControls =
                   UserInputControls.NoNegativeResponseAccepted |
                   UserInputControls.NoZeroResponseAccepted;
               PromptPointResult pdr = prompts.AcquirePoint(jpdo);
               if (pdr.Status == PromptStatus.OK)
               {
                   if (pdr.Value == m_temp)
                       return SamplerStatus.NoChange;
                   else
                   {
                       m_temp = pdr.Value;
                       m_center = m_temp;
                       m_rad = (m_initial.GetVectorTo(m_center)).Length;
                       if (m_rad < m_tol.EqualVector) m_rad = m_tol.EqualVector;
                       return SamplerStatus.OK;
                   }
               }
               return SamplerStatus.Cancel;
           }

           public Entity GetEntity()
           {
               return Entity;
           }
       }




       class CircPlaceJig : EntityJig
       {

           Point3d m_cenPt;
           Point3d m_prev;
           Matrix3d m_ucs;

           public CircPlaceJig(Entity ent, Point3d cenPoint, Matrix3d ucs)
               : base(ent.Clone() as Entity)
           {
               m_prev = cenPoint;
               m_ucs = ucs;
           }

           protected override SamplerStatus Sampler(JigPrompts jp)
           {
               JigPromptPointOptions jppo = new JigPromptPointOptions("\nLocation of Circle: ");
               PromptPointResult ppr = jp.AcquirePoint(jppo);
               if (ppr.Status == PromptStatus.OK)
               {
                   if (m_prev == ppr.Value)
                   {
                       return SamplerStatus.NoChange;
                   }

                   else
                   {
                       m_cenPt = ppr.Value;
                       return SamplerStatus.OK;
                   }
               }
               return SamplerStatus.Cancel;
           }



           protected override bool Update()
           {

               Matrix3d trans = Matrix3d.Displacement(m_cenPt - m_prev);

               Entity.TransformBy(trans);
               m_prev = m_cenPt;
               return true;

           }

           public Entity GetEntity()
           {
               return Entity;
           }
       }
   }
}

Posted

Nice Keant,

 

Probably due to my poor English i misunderstood the OP request...

 

I notice an exception can occur if the circle radius is equal to 0.

I solve it in the SamplerStatus function adding a test:

 

if (pdr.Value == m_initial || pdr.Value == m_temp)
   return SamplerStatus.NoChange;

Posted

I’m not really certain about the original request parameters. To be honest, I doubt very much that my routine matches those parameters either. It was a fun notion to play with nonetheless.

Posted

Hi SEANT,

Interesting routine you've got there. I played around with that for a while.

What I need is simpler I think: The radius is fixed, the user does not change it. So I just need the fixed circle to move around with the mouse-pointer. What would be nice is a line from the initial point to the centre of the circle.

I'm going to try and modify what you've posted.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...