.NET

.NET

Reply
Active Member
JoseVicario
Posts: 6
Registered: ‎08-20-2012
Message 1 of 6 (855 Views)
Accepted Solution

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

855 Views, 5 Replies
08-20-2012 03:35 AM

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

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-with-arc-segments-using-net.html

 

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.

 

ADN Support Specialist
Balaji_Ram
Posts: 749
Registered: ‎03-21-2011
Message 2 of 6 (792 Views)

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

08-27-2012 10:02 AM 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

Active Member
JoseVicario
Posts: 6
Registered: ‎08-20-2012
Message 3 of 6 (770 Views)

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

08-28-2012 12:40 AM in reply to: Balaji_Ram

Thank You for answer. Perfect solution!

Mentor
SRSDS
Posts: 256
Registered: ‎04-15-2011
Message 4 of 6 (452 Views)

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

05-20-2013 02:58 PM 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?

Distinguished Mentor
gasty1001
Posts: 582
Registered: ‎04-11-2010
Message 5 of 6 (447 Views)

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

05-20-2013 04:30 PM 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

Mentor
SRSDS
Posts: 256
Registered: ‎04-15-2011
Message 6 of 6 (425 Views)

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

05-21-2013 08:02 AM in reply to: gasty1001

Thanks Gaston!

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.