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

How to jig a Spline (fit points) using AutoCAD 2013 .NET API?

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
JoseVicario
1731 Views, 5 Replies

How to jig a Spline (fit points) using AutoCAD 2013 .NET API?

How can I draw Spline with fit points in AutoCAD 2013 .NET API using my own jig?

5 REPLIES 5
Message 2 of 6
Balaji_Ram
in reply to: JoseVicario

Hi,

 

Here is a sample code that might help in getting started with jigging of spline. It is a modified version of the code from this blog post dealing with polyline jigging.

http://through-the-interface.typepad.com/through_the_interface/2010/12/jigging-an-autocad-polyline-w...

 

Here is the code :

using System;
using System.Windows.Forms;

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

[assembly: CommandClass(typeof(SplineJig.SplineJigCmds))]

namespace SplineJig
{
	public class SplineDrawJig : DrawJig
	{
		// members
		private Point3d _point;
		private Point3dCollection _points = new Point3dCollection();

		public Point3dCollection Coordinates
		{
			get
			{
				return _points;
			}
		}

		public SplineDrawJig()
		{
		}

		public void DoIt()
		{
			Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
			PromptResult jigRes = null;

			do
			{
				jigRes = ed.Drag(this);
				if (jigRes.Status == PromptStatus.OK)
				{
					_points.Add(_point);
				}
				else if (jigRes.Status == PromptStatus.None)
				{
					break;
				}
			}
			while (jigRes.Status == PromptStatus.OK);
		}

		protected override Autodesk.AutoCAD.EditorInput.SamplerStatus
		Sampler(Autodesk.AutoCAD.EditorInput.JigPrompts prompts)
		{
			JigPromptPointOptions jigOpts = new JigPromptPointOptions();

			jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates | UserInputControls.NullResponseAccepted);
			jigOpts.Message = "Select fit point: ";
			PromptPointResult jigRes = prompts.AcquirePoint(jigOpts);
			Point3d pt = jigRes.Value;

			if (pt == _point)
			{
				return SamplerStatus.NoChange;
			}
			_point = pt;
			if (jigRes.Status == PromptStatus.OK)
			{
				return SamplerStatus.OK;
			}
			return SamplerStatus.Cancel;
		}

		protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
		{
			if (_points.Count == 0)
			{
				return true;
			}
			Point3dCollection vertices = new Point3dCollection();

			foreach (Point3d point in _points)
			{
				vertices.Add(point);
			}
			vertices.Add(_point);

			int totalVertices = vertices.Count;
			if(totalVertices >= 2)
			{
				Line chordLine = new Line(vertices[totalVertices - 1], vertices[totalVertices-2]);
				draw.Geometry.Draw(chordLine);
			}
 
			Spline spline = new Spline(vertices, KnotParameterizationEnum.Chord, 3, 0.0);
			draw.Geometry.Draw(spline);
			
			return true;
		}
	}

	public class SplineJigCmds
	{
		public SplineJigCmds() { }

		[CommandMethod("SplineJig")]
		static public void SplineJig()
		{
			try
			{
				Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
				Database db = ed.Document.Database;
				SplineDrawJig splineJig = new SplineDrawJig();

				splineJig.DoIt();

				// order of the curve can be between 2 and 26
                int order = 6;
                // determines extent of interpolation through all the points 
                double fitTolerance = .5;

				Spline spline = new Spline(splineJig.Coordinates, KnotParameterizationEnum.Chord, 3, 0.0);
				using (Transaction tr = db.TransactionManager.StartTransaction())
				{
					BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

					btr.AppendEntity(spline);
					tr.AddNewlyCreatedDBObject(spline, true);
					tr.Commit();
				}
			}
			catch (System.Exception ex)
			{
				MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
	}
}

 Hope this helps.

 



Balaji
Developer Technical Services
Autodesk Developer Network

Message 3 of 6
JoseVicario
in reply to: Balaji_Ram

Thank You for answer. Perfect solution!

Message 4 of 6
SRSDS
in reply to: JoseVicario

I'd like to add an rotated entity to the ends of the spline.

The Spline.StartFitTangent is giving a result of (0,0,0)

Is there another way of getting the angle of the start and endpoints?

Message 5 of 6
hgasty1001
in reply to: SRSDS

Hi,

 

you can obtain the tangent angle from the first derivative of the curve with: MySpline.GetFirstDerivative(AtSomePoint), check the docs for others method that the curve class have.

 

Gaston Nunez

Message 6 of 6
SRSDS
in reply to: hgasty1001

Thanks Gaston!

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