Creating an AutoCAD multileader spraying out from central text using .NET
2007-09-07 10:58
555 查看
Some more fun with multileaders... this post shows some code that adds multiple leader lines to a multileader/MLeader, each of them "spraying" out from the central text. We ask the user for the central point and the location of the first leader along with the number of leader lines to "spray". These lines then get created at evenly spaced angles around from this initial location.
I can't think of anything immediately useful about this technique, other than it was fun to write the code and it shows how to add multiple leader lines. It also generates some fun results. :-)
Here's the C# code:
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
namespace DimensionLibrary
{
public class DimensionCommands
{
[CommandMethod("spray")]
static public void SprayMultiLeader()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
// Get the start point of the leader
PromptPointResult ppr =
ed.GetPoint(
"/nSpecify position of text: "
);
if (ppr.Status != PromptStatus.OK)
return;
Point3d startPt = ppr.Value;
// Get the end point of the leader
PromptPointOptions ppo =
new PromptPointOptions(
"/nSpecify first arrowhead location: "
);
ppo.BasePoint = startPt;
ppo.UseBasePoint = true;
ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK)
return;
Point3d endPt = ppr.Value;
// Get the number of leader lines
// to "spray" out from the center
PromptIntegerOptions pio =
new PromptIntegerOptions(
"/nNumber of leader lines <1>: "
);
pio.AllowNone = true;
pio.AllowZero = false;
PromptIntegerResult pir =
ed.GetInteger(pio);
int numLines;
if (pir.Status == PromptStatus.None)
numLines = 1;
else
{
if (pir.Status != PromptStatus.OK)
return;
else
numLines = pir.Value;
}
Transaction tr =
db.TransactionManager.StartTransaction();
using (tr)
{
try
{
BlockTable bt =
(BlockTable)tr.GetObject(
db.BlockTableId,
OpenMode.ForRead
);
BlockTableRecord btr =
(BlockTableRecord)tr.GetObject(
bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite
);
// Create the MLeader
MLeader mld = new MLeader();
for(int i=0; i < numLines; i++)
{
// Each leader line sprays out
// at a different angle
Vector3d vec = endPt - startPt;
vec =
vec.RotateBy(
i * 2 * System.Math.PI / numLines,
mld.Normal
);
Point3d landPt = startPt + vec;
int ldNum = mld.AddLeader();
int lnNum = mld.AddLeaderLine(ldNum);
mld.AddFirstVertex(lnNum, landPt);
mld.AddLastVertex(lnNum, startPt);
}
mld.LeaderLineType =
LeaderType.SplineLeader;
// Create the MText
MText mt = new MText();
mt.Contents =
"Multileader with " +
numLines.ToString() +
" /nradial splines";
mld.ContentType =
ContentType.MTextContent;
mld.MText = mt;
mld.TextLocation = startPt;
// Add the MLeader
btr.AppendEntity(mld);
tr.AddNewlyCreatedDBObject(mld, true);
tr.Commit();
}
catch (Exception ex)
{
ed.WriteMessage("/nException: " + ex.Message);
// Would also happen automatically
// if we didn't commit
tr.Abort();
}
}
}
}
}
Here's a 7-line multileader created by the SPRAY command:
![](http://through-the-interface.typepad.com/through_the_interface/images/2007/09/05/mleader_multiple_segment_spline.png)
相关文章推荐
- Creating a multileader in AutoCAD using a jig from .NET
- Creating a spline-segment multileader in AutoCAD using .NET
- Creating an AutoCAD table linked to an Excel spreadsheet using .NET
- Updating an Excel spreadsheet from a linked AutoCAD table using .NET
- Using a palette from .NET to display properties of multiple AutoCAD objects
- Driving a multi-sheet AutoCAD plot using .NET
- ASP.NET datagrid直接导出为Excel Export to Excel from DataGrid (Using an Microsoft Access database)
- Updating a specific attribute inside an AutoCAD drawing using .NET
- Purging registered application names from a folder of AutoCAD drawings using .NET
- http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
- Populate an ASP.NET ListBox from JSON results using jQuery
- 转 Adding items to an AutoCAD tool palette using .NET
- Accessing the active space or layout in an AutoCAD drawing using .NET
- Allowing selection of an AutoCAD plot device and media name using .NET
- Updating an AutoCAD table linked to an Excel spreadsheet using .NET
- Updating a specific attribute inside a folder of AutoCAD drawings using RealDWG from .NET
- Plotting a window from AutoCAD using .NET
- Moving entities from one AutoCAD layer to another using .NET
- C# .NET - Sql Bulk Insert from multiple delimited Textfile using c#.net
- Driving a basic AutoCAD plot using .NET