CADTutor: The best free help for AutoCAD on the web

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
Go Back   AutoCAD Forums > AutoCAD > AutoLISP, VBA, the CUI & Customisation

Reply
 
Thread Tools
Old 4th Nov 2009, 11:18 am   #1
Jozi68
Full Member
 
Using: AutoCAD 2009
 
Join Date: Apr 2009
Posts: 64
Default VB.net: Create a circle

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.
Jozi68 is offline   Reply With Quote
Old 4th Nov 2009, 11:58 am   #2
SEANT
Super Member
 
SEANT's Avatar
 
Using: Mechanical 2009
 
Join Date: Aug 2005
Location: Rhode Island
Posts: 1,184
Default

The .Net term for that activity is “Jigging”. Kean Walmsley has some good examples here.

http://through-the-interface.typepad...nterface/jigs/
SEANT is online now   Reply With Quote
Old 6th Nov 2009, 12:19 pm   #3
Jozi68
Full Member
 
Using: AutoCAD 2009
 
Join Date: Apr 2009
Posts: 64
Default

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.
Jozi68 is offline   Reply With Quote
Old 6th Nov 2009, 01:51 pm   #4
gile
Full Member
 
Using: AutoCAD 2007
 
Join Date: Jul 2009
Location: Marseille France
Posts: 85
Default

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.

Code:
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();
                }
            }
        }
    }
}
gile is offline   Reply With Quote
Old 6th Nov 2009, 05:02 pm   #5
SEANT
Super Member
 
SEANT's Avatar
 
Using: Mechanical 2009
 
Join Date: Aug 2005
Location: Rhode Island
Posts: 1,184
Default

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.


Code:
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;
            }
        }
    }
}

Last edited by SEANT : 6th Nov 2009 at 08:51 pm.
SEANT is online now   Reply With Quote
Old 6th Nov 2009, 06:26 pm   #6
gile
Full Member
 
Using: AutoCAD 2007
 
Join Date: Jul 2009
Location: Marseille France
Posts: 85
Default

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:

Code:
if (pdr.Value == m_initial || pdr.Value == m_temp)
    return SamplerStatus.NoChange;
gile is offline   Reply With Quote
Old 6th Nov 2009, 06:49 pm   #7
SEANT
Super Member
 
SEANT's Avatar
 
Using: Mechanical 2009
 
Join Date: Aug 2005
Location: Rhode Island
Posts: 1,184
Default

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.

Last edited by SEANT : 6th Nov 2009 at 11:22 pm.
SEANT is online now   Reply With Quote
Old 9th Nov 2009, 07:05 am   #8
Jozi68
Full Member
 
Using: AutoCAD 2009
 
Join Date: Apr 2009
Posts: 64
Default

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.
Jozi68 is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Circle Solid? mrjagsr744 AutoCAD 3D Modelling & Rendering 2 24th Feb 2009 03:27 pm
Create a circle with a width - UPDATED AutoCAD Tips Blog AutoCAD RSS Feeds 0 7th Dec 2008 10:40 pm
Create a circle with a width AutoCAD Tips Blog AutoCAD RSS Feeds 0 30th Nov 2007 11:10 pm
Bottom as box and top as like circle Adesu AutoCAD 3D Modelling & Rendering 9 16th Aug 2007 12:59 am
Line from inner circle to outer circle jocaan AutoCAD Beginners' Area 6 11th Mar 2004 09:28 pm

Why Donate?


All times are GMT +1. The time now is 11:28 am.

RSS Feed for AutoCAD ForumsValid XHTML 1.0!Valid CSS!Creative Commons Licence