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

Pline jig with double

1 REPLY 1
Reply
Message 1 of 2
kresimir.kukec
1186 Views, 1 Reply

Pline jig with double

hi 

I want to modify Kean Walmsely polyline Jig. For before every point user will be asked for entering double (nadsloj) and this double will be subtacted form points y value (cent). 

Here is class code:

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace prolazi
{
    class PlineJig : EntityJig
    {
        // Maintain a list of vertices...
        // Not strictly necessary, as these will be stored in the
        // polyline, but will not adversely impact performance
        Point3dCollection m_pts;
        // Use a separate variable for the most recent point...
        // Again, not strictly necessary, but easier to reference
        Point3d m_tempPoint;
        Plane m_plane;
        double nadsloj = 1;
        double cent;
        double promjerC = 300;

        public PlineJig(Matrix3d ucs)
            : base(new Polyline())
        {
            // Create a point collection to store our vertices
            m_pts = new Point3dCollection();

            // Create a temporary plane, to help with calcs
            Point3d origin = new Point3d(0, 0, 0);
            Vector3d normal = new Vector3d(0, 0, 1);
            normal = normal.TransformBy(ucs);
            m_plane = new Plane(origin, normal);

            // Create polyline, set defaults, add dummy vertex
            Polyline pline = Entity as Polyline;
            pline.SetDatabaseDefaults();
            pline.Normal = normal;
            pline.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
        }

        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;
            JigPromptPointOptions jigOpts =
              new JigPromptPointOptions();
            jigOpts.UserInputControls =
              (UserInputControls.Accept3dCoordinates |
              UserInputControls.NullResponseAccepted |
              UserInputControls.NoNegativeResponseAccepted
              );
            
            //JigPromptOptions jpo = new JigPromptOptions();


            if (m_pts.Count == 0)
            {
                
                // For the first vertex, just ask for the point
                jigOpts.Message =
                  "\nStart point of polyline: ";
                
            }
            else if (m_pts.Count > 0)
            {
                //cent = funkcije.odnadsloj(nadsloj) + promjerC * 0.0005;
                // For subsequent vertices, use a base point
                jigOpts.BasePoint = m_pts[m_pts.Count - 1];
                jigOpts.UseBasePoint = true;
                jigOpts.Message = "\nPolyline vertex: ";
            }
            else // should never happen
                return SamplerStatus.Cancel;

            // Get the point itself
            
            PromptPointResult res =
              prompts.AcquirePoint(jigOpts);
            // Check if it has changed or not
            // (reduces flicker)
            if (m_tempPoint == res.Value)
            {
                return SamplerStatus.NoChange;
            }
            else if (res.Status == PromptStatus.OK)
            {
                
                m_tempPoint = res.Value;
                //m_tempPoint = new Point3d(res.Value.X, res.Value.Y - cent, 0);
                return SamplerStatus.OK;
            }
            return SamplerStatus.Cancel;
        }

        protected override bool Update()
        {
            // Update the dummy vertex to be our
            // 3D point projected onto our plane
            Polyline pline = Entity as Polyline;
            pline.SetPointAt(
              pline.NumberOfVertices - 1,
              m_tempPoint.Convert2d(m_plane)
            );
            return true;
        }

        public Entity GetEntity()
        {
            return Entity;
        }

        public void AddLatestVertex()
        {

            cent = funkcije.odnadsloj(nadsloj) + promjerC * 0.0005;
            Point3d k = new Point3d(m_tempPoint.X, m_tempPoint.Y - cent, 0);
           //_tempPoint = k;
            m_pts.Add(k);
            Polyline pline = Entity as Polyline;
            // Create a new dummy vertex...
            // can have any initial value
            pline.AddVertexAt(
              pline.NumberOfVertices,
              new Point2d(0, 0),
              0, 0, 0
            );
        }

        public void RemoveLastVertex()
        {
            // Let's remove our dummy vertex
            Polyline pline = Entity as Polyline;
            pline.RemoveVertexAt(m_pts.Count);
        }
    }
}

 Here is function code:

[CommandMethod("MYPOLY")]
        public void MyPolyJig()
        {
            Document doc =
              Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            // Get the current UCS, to pass to the Jig
            Matrix3d ucs =
              ed.CurrentUserCoordinateSystem;

            // Create our Jig object
            PlineJig jig = new PlineJig(ucs);

            // Loop to set the vertices directly on the polyline
            bool bSuccess = true, bComplete = false;
            do
            {
                PromptResult res = ed.Drag(jig);
                bSuccess =
                  (res.Status == PromptStatus.OK);
                // A new point was added
                if (bSuccess)
                    jig.AddLatestVertex();
                // Null input terminates the command
                bComplete =
                  (res.Status == PromptStatus.None);
                if (bComplete)
                    // Let's clean-up the polyline before adding it
                    jig.RemoveLastVertex();
            } while (bSuccess && !bComplete);

            // If the jig completed successfully, add the polyline
            if (bComplete)
            {
                // Append entity
                Database db = doc.Database;
                Transaction tr =
                  db.TransactionManager.StartTransaction();
                using (tr)
                {
                    BlockTable bt =
                      (BlockTable)tr.GetObject(
                        db.BlockTableId,
                        OpenMode.ForRead,
                        false
                      );
                    BlockTableRecord btr =
                      (BlockTableRecord)tr.GetObject(
                        bt[BlockTableRecord.ModelSpace],
                        OpenMode.ForWrite,
                        false
                      );
                    btr.AppendEntity(jig.GetEntity());
                    tr.AddNewlyCreatedDBObject(jig.GetEntity(), true);
                    tr.Commit();
                }
            }
        }

 I tried everthing is it posable to do this?

 

1 REPLY 1
Message 2 of 2
Balaji_Ram
in reply to: kresimir.kukec

Hello,

 

Here is a sample code to demonstrate acquiring a point and double input alternately using a jig. 

It aquires the width for each vertex point of the polyline and sets the polyline width at the vertex.

 

This code does not alter the y coordinate as you seem to be trying. But I think this code can help as a starting point for your implementation.

 

 

class PlineJig : EntityJig
{
    Point3dCollection m_pts;
    Point3d m_tempPoint;
    double m_Width = 1.0;
    Plane m_plane;

    public bool InputAcquirePoint { get; set; }

    public PlineJig(Matrix3d ucs) : base(new Polyline())
    {
        m_pts = new Point3dCollection();

        Point3d origin = new Point3d(0, 0, 0);
        Vector3d normal = new Vector3d(0, 0, 1);
        normal = normal.TransformBy(ucs);
        m_plane = new Plane(origin, normal);

        Polyline pline = Entity as Polyline;
        pline.SetDatabaseDefaults();
        pline.Normal = normal;
        pline.AddVertexAt(0, new Point2d(0, 0), 0, 0, 0);
    }

    protected override SamplerStatus Sampler(JigPrompts prompts)
    {
        Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        Editor ed = doc.Editor;

        if (InputAcquirePoint)
        {
            JigPromptPointOptions jigOpts = new JigPromptPointOptions();
            jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates | UserInputControls.NullResponseAccepted | UserInputControls.NoNegativeResponseAccepted);
            if (m_pts.Count == 0)
            {
                // For the first vertex, just ask for the point
                jigOpts.Message = "\nStart point of polyline: ";
            }
            else if (m_pts.Count > 0)
            {
                // For subsequent vertices, use a base point
                jigOpts.BasePoint = m_pts[m_pts.Count - 1];
                jigOpts.UseBasePoint = true;
                jigOpts.Message = "\nNext point of the Polyline : ";
            }
            else // should never happen
                return SamplerStatus.Cancel;

            // Get the point itself
            PromptPointResult res = prompts.AcquirePoint(jigOpts);
            if (m_tempPoint == res.Value)
            {
                return SamplerStatus.NoChange;
            }

            else if (res.Status == PromptStatus.OK)
            {
                m_tempPoint = res.Value;
                return SamplerStatus.OK;
            }
        }
        else
        {
            JigPromptDistanceOptions jigOpts = new JigPromptDistanceOptions();
            jigOpts.BasePoint = m_pts[m_pts.Count - 1];
            jigOpts.UseBasePoint = true;
            jigOpts.Message = "\nWidth : ";
            PromptDoubleResult pdr = prompts.AcquireDistance(jigOpts);
            if (m_Width == pdr.Value)
            {
                return SamplerStatus.NoChange;
            }
            else if (pdr.Status == PromptStatus.OK)
            {
                m_Width = pdr.Value;
                return SamplerStatus.OK;
            }
        }
        return SamplerStatus.Cancel;
    }

    protected override bool Update()
    {
        // Update the dummy vertex to be our
        // 3D point projected onto our plane
        Polyline pline = Entity as Polyline;
        pline.SetPointAt(pline.NumberOfVertices - 1, m_tempPoint.Convert2d(m_plane));
        return true;
    }

    public Entity GetEntity()
    {
        return Entity;
    }

    public void AddLatestVertex()
    {   
        Point3d k = new Point3d(m_tempPoint.X, m_tempPoint.Y, 0);
        m_pts.Add(k);
        Polyline pline = Entity as Polyline;

        // Create a new dummy vertex...
        // can have any initial value
        pline.AddVertexAt(pline.NumberOfVertices, new Point2d(0, 0), 0, m_Width, m_Width);
    }

    public void RemoveLastVertex()
    {
        // Let's remove our dummy vertex
        Polyline pline = Entity as Polyline;
        pline.RemoveVertexAt(m_pts.Count);
    }

    public void AddLatestWidth()
    {
        Polyline pline = Entity as Polyline;

        if (m_pts.Count == 1)
        {
            pline.SetStartWidthAt(m_pts.Count - 1, m_Width);
            pline.SetEndWidthAt(m_pts.Count - 1, m_Width);
        }
        else
        {
            pline.SetStartWidthAt(m_pts.Count - 1, m_Width);
            pline.SetEndWidthAt(m_pts.Count - 1, m_Width);
            pline.SetEndWidthAt(m_pts.Count - 2, m_Width);
        }
    }
}

 

Here is the code to use the Jig class

 

    [CommandMethod("MYPOLY")]
    public void MyPolyJig()
    {
        Document doc =
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
        Editor ed = doc.Editor;

        // Get the current UCS, to pass to the Jig
        Matrix3d ucs = ed.CurrentUserCoordinateSystem;

        // Create our Jig object
        PlineJig jig = new PlineJig(ucs);
        jig.InputAcquirePoint = true;

        // Loop to set the vertices directly on the polyline
        bool bSuccess = true, bComplete = false;
        do
        {
            PromptResult res = ed.Drag(jig);
            bSuccess = (res.Status == PromptStatus.OK);

            // A new point was added
            if (bSuccess)
            {
                if(jig.InputAcquirePoint)
                    jig.AddLatestVertex();
                else
                    jig.AddLatestWidth();

                jig.InputAcquirePoint = ! jig.InputAcquirePoint;
            }
                
            // Null input terminates the command
            bComplete = (res.Status == PromptStatus.None);
            if (bComplete)
                // Let's clean-up the polyline before adding it
                jig.RemoveLastVertex();
        } while (bSuccess && !bComplete);

        // If the jig completed successfully, add the polyline
        if (bComplete)
        {
            // Append entity
            Database db = doc.Database;
            Transaction tr =
                db.TransactionManager.StartTransaction();
            using (tr)
            {
                BlockTable bt =
                    (BlockTable)tr.GetObject(
                    db.BlockTableId,
                    OpenMode.ForRead,
                    false
                    );
                BlockTableRecord btr =
                    (BlockTableRecord)tr.GetObject(
                    bt[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite,
                    false
                    );
                btr.AppendEntity(jig.GetEntity());
                tr.AddNewlyCreatedDBObject(jig.GetEntity(), true);
                tr.Commit();
            }
        }
    }
}

 

Regards,

Balaji

 

 

 

 

 



Balaji
Developer Technical Services
Autodesk Developer Network

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