static public Arc Arc(Point3d Cen,Vector3d V, double Rad,double startAng,double endAng,string sOwner)
//standard arc
static public Arc Arc(Center Center, Point3d StartP, Point3d EndP, string sOwner)
//3 point arc with center
static public Arc Arc( Point3d StartP,Point3d MidP, Point3d EndP, string sOwner)
//3 point arc
public enum ArcCreationMethod
{
CenterAndEndpoints,
ThreePointCurve
}
static public Arc Arc(ArcCreationMethod method, Point3d param1, Point3d param2, Point3d param3, string sOwner)
static private void AddEntTtoDb(Entity Ent, string sOwner)
{
BlockTable bt;
BlockTableRecord btr;
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
switch (sOwner)
{
case "Ms":
btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
break;
case "Ps":
btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.PaperSpace], OpenMode.ForWrite);
break;
case "Cs":
btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
break;
default:
if ((bt.Has(sOwner)))
{
btr = (BlockTableRecord)trans.GetObject(bt[sOwner], OpenMode.ForWrite);
Util.GetandPrint.Print("The block " + btr.Name + " exists");
}
else
{
btr = new BlockTableRecord();
btr.Name = sOwner;
bt.Add(btr);
trans.AddNewlyCreatedDBObject(btr, true);
}
break;
}
btr.AppendEntity(Ent);
trans.AddNewlyCreatedDBObject(Ent, true);
trans.Commit();
}
BlockTableRecord modelSpaceRecord1 = (BlockTableRecord)tr.GetObject(
bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite);
BlockTableRecord modelSpaceRecord2 = (BlockTableRecord)tr.GetObject(
bt["*MODEL_SPACE"],
OpenMode.ForWrite);
.... but I'd personally pass the ObjectID of the container I wanted the Ent added to .. as Glenn mentioned.
public class Arc
{
; this is a constructor
public Arc()
{
}
}
using SomethingWithAnArcClass;
namespace Util;
{
public class AddEnt()
{
; this is the constructor for the AddEnt class
public AddEnt()
{
}
; this is NOT a constructor
public Arc Arc()
{
}
}
}
In any case, the only way to use method overloading is to have arguments with distinct types. That's how overloading works. But overloading is simply a convenience for the programmer; it is not a goal. You are gaining nothing good by trying to FORCE your code to use overloading. All overloading lets you do is avoid having a lot of methods like ArcFromStartAndEndAngles(), ArcFromCenterAndTwoPoints(), and Arc3P(). Overloading lets you use the same name for the first two. But because the arguments are the same, you can't use operator overloading for the last one. Instead, you can use the names Arc(), Arc(), and Arc3P(), and the compiler will be able to tell the difference between the first two methods by looking at the arguments.static private void AddEntTtoDb(Entity Ent, string sOwner)
I do not know what you are doing...Quotestatic private void AddEntTtoDb(Entity Ent, string sOwner)
If you have a chance, look for the "MgdDbg" project by Jim Awe from Adesk, and read the "SymTbl.cs"
http://discussion.autodesk.com/thread.jspa?messageID=4907654
hth
Glenn, since there is virtually no globals I have a sub that is the full monty way of adding an ent. Otherwise I was finding C# was way more verbose than vba. With as BlockTableRecord btr here and a BlockTableRecord btr there etc.
I traversed this same path, asked myself the same questions you have asked yourself, and lamented all the extra work involved in using this API. But in the end, I agree with Tony.Glenn, since there is virtually no globals I have a sub that is the full monty way of adding an ent. Otherwise I was finding C# was way more verbose than vba. With as BlockTableRecord btr here and a BlockTableRecord btr there etc.
Well, if I may say so myself, I've found that trying to invent
your own API that offers the 'full monty' way of doing things,
will ultimately create more problems than it solves.
<snip>
namespace CaddZone.AutoCAD.EditorServices
{
////////////////////////////////////////////////////////////////////
// A class that implements a command to reset the color
// of selected block references to BYLAYER:
public class BlockRefPropertyReset : SelectionEditor<BlockReference>
{
protected override void ProcessEntity(BlockReference blockref)
{
blockref.ColorIndex = 256;
return EditorStatus.Continue;
}
[CommandMethod("BLOCKCOLORTOBYLAYER", CommandFlags.UsePickSet)]
public override void CommandHandler()
{
Execute();
}
}
///////////////////////////////////////////////////////////
/// CIRCLEOFFSET command:
///
/// This specialization of SelectionEditor implements
/// an editor that prompts you to select one or more
/// circles, and enter a distance. It then offsets each
/// selected circle's radius by the specified distance.
///
/// The larger thing which the trained eye will notice
/// about this, is that we have successfuly factored
/// out 95% of the 'grunt work' that must typically
/// be done in order to build relatively simple custom
/// AutoCAD commands, using the .NET API.
///
public class CircleOffsetEditor : SelectionEditor<Circle>
{
// Called after objects are selected, to
// obtain additional input:
protected override PromptStatus EndEditorInput( Editor ed )
{
PromptDistanceOptions ops = new PromptDistanceOptions( "" );
ops.Message = "\nCircle radius offset: ";
ops.AllowZero = false;
ops.DefaultValue = offset;
PromptDoubleResult res = ed.GetDistance( ops );
if( res.Status == PromptStatus.OK )
offset = res.Value; // store the value for use in ProcessObject()
return res.Status;
}
/// <summary>
/// Override this to gain access to the PromptSelectionOptions
/// that's used to get the selection of objects. Here we just
/// set the prompts to "Select/remove circles: "
/// </summary>
protected override void BeginSelect( PromptSelectionOptions options )
{
base.BeginSelect( options );
options.MessageForAdding = "Select circles: ";
options.MessageForRemoval = "Remove circles: ";
}
/// <summary>
/// This override is called once for each selected
/// object, which is passed in as the parameter.
///
/// The passed in object is already open for Write,
/// and cast to the Type of the generic parmeter
/// argument to this class (a Circle in this case).
///
/// This is where each object is Modified.
///
/// </summary>
protected override EditorStatus ProcessEntity( Circle circle )
{
if( circle.Radius + offset > 0.0 )
circle.Radius += offset;
return EditorStatus.Continue; // continue processing objects
}
// user-supplied offset distance:
private double offset = 1.0;
// Implement a command for this editor
[CommandMethod("CIRCLEOFFSET", CommandFlags.UsePickSet)]
public override void CommandHandler()
{
Execute();
}
}
////////////////////////////////////////////////////////////////////
// A class that implements the 'LENGTH' command, which
// measures the length of selected curves:
public class CurveLengthReporter : SelectionViewer<Curve>
{
protected override void Initialize()
{
totalLength = 0.0;
counted = 0;
base.Initialize();
}
protected override EditorStatus ProcessEntity( Curve curve )
{
try // Ignore eNotApplicable returned by rays/xlines
{
totalLength += curve.GetDistanceAtParameter( curve.EndParam );
++counted;
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
if( ex.ErrorStatus != ErrorStatus.NotApplicable )
throw ex;
}
return EditorStatus.Continue;
}
protected override string GetReportMessage()
{
if( counted > 0 )
{
return string.Format(
"Total length of {0} curves: {1}, average length: {2}",
counted, totalLength, totalLength / counted );
}
else
return "No curves were processed";
}
[CommandMethod( "LENGTH", CommandFlags.UsePickSet )]
public override void CommandHandler()
{
Execute();
}
private Distance totalLength = 0.0;
private int counted = 0;
}
//////////////////////////////////////////////////////////////
/// The SCALEWIDTH command
/// <summary>
///
/// The SCALEWIDTH command applies a scale factor to the
/// constant widths of a selection of polyLines.
///
/// Custom filtering:
///
/// This class also utilizes custom selection filtering, where
/// an overridden method of the base class examines each
/// object selected by the user, and acccepts or rejects it.
/// Custom filtering is enabled by overriding the FilterObject()
/// method. At runtime the SelectionEditor class will detect if
/// this method has been overridden in a derived class, and if
/// so, it will invoke the method to allow it to filter each object
/// selected by the user.
public class PolylineWidthScaler : SelectionEditor<Polyline>
{
// Get the scale factor to apply to each Polyline's width:
protected override PromptStatus EndEditorInput( Editor editor )
{
PromptDoubleOptions ops = new PromptDoubleOptions( "\nWidth scale factor: " );
ops.AllowNegative = false;
ops.AllowZero = false;
ops.DefaultValue = scaleFactor;
PromptDoubleResult res = editor.GetDouble( ops );
if( res.Status != PromptStatus.OK )
return res.Status;
if( Math.Abs( res.Value - 1.0 ) < 1.0e-6 )
{
Prompt( "\nScale factor cannot be 1.0," );
return PromptStatus.Other;
}
scaleFactor = res.Value;
return res.Status;
}
// Filter out polyLines whose constant width is nominally 0
protected override bool FilterEntity( Polyline polyline )
{
return polyline.ConstantWidth > 1.0e-6;
}
// Operate on each Polyline
protected override EditorStatus ProcessEntity( Polyline polyline )
{
polyline.ConstantWidth *= scaleFactor;
return EditorStatus.Continue;
}
[CommandMethod( "SCALEWIDTH", CommandFlags.UsePickSet )]
public override void CommandHandler()
{
base.CommandHandler();
}
private double scaleFactor = 1.0;
}
// only to show how rediculously simple it is to build commands
// with SelectionEditor<>, this example prompts for a selection set
// of circles, and changes their color to RED, and does it in only 4
// lines of code:
public class MakeCirclesRed : SelectionEditor<Circle>
{
protected override EditorStatus ProcessEntity(Circle circle)
{
circle.ColorIndex = 1;
return EditorStatus.Continue;
}
}
// To invoke the command, just use: new MakeCirclesRed().Execute();
}
I traversed this same path, asked myself the same questions you have asked yourself, and lamented all the extra work involved in using this API. But in the end, I agree with Tony.Glenn, since there is virtually no globals I have a sub that is the full monty way of adding an ent. Otherwise I was finding C# was way more verbose than vba. With as BlockTableRecord btr here and a BlockTableRecord btr there etc.
Well, if I may say so myself, I've found that trying to invent
your own API that offers the 'full monty' way of doing things,
will ultimately create more problems than it solves.
<snip>
We all know however that we are lazy (http://bobbycjones.spaces.live.com/blog/cns!2A36783BF92E8178!167.entry) and typing is not how we want to spend our days. If you're using Visual Studio as your IDE, I suggest Code Snippets (http://bobbycjones.spaces.live.com/blog/cns!2A36783BF92E8178!170.entry). You can create snippets that encapsulate a logical action, like adding an entity to a database, although most of mine aren't quite that chunky, and modify the code after it's inserted.
Code snippits are great for inserting simple frequently used constructs,
like try/finally try/catch and so on. But, I don't know if I would use it
to replicate more complex, commonly used constructs rather than
abstracting that into a reusable form, which eliminates the need for
replication (replication of code is something I try to avoid as much as
possible).
.............>>> <<<
So, while there's nothing wrong with snippets for reducing typing,
I think they can also be misused to replicate larger code constructs
that possibly shouldn't be replicated if that can be avoided through
carefully designed modular, reusable classes.
// class Autodesk.AutoCAD.Runtime.Converter
// enum Autodesk.AutoCAD.Runtime.DistanceUnitFormat
// struct Autodesk.AutoCAD.Geometry.Point3d
//--------
public static string PtToStr(Point2d pt)
{
return PtToStr(pt, DistanceUnitFormat.Current, -1);
}
//--------
public static string PtToStr(Point3d pt)
{
return PtToStr(pt, DistanceUnitFormat.Current, -1);
}
//--------
public static string PtToStr(Point2d pt, DistanceUnitFormat unitType, int prec)
{
return string.Format("({0}, {1})",
Converter.DistanceToString(pt.X, unitType, prec),
Converter.DistanceToString(pt.Y, unitType, prec));
}
/// <summary>
///
/// </summary>
/// <param name="pt"></param>
/// <param name="unitType"></param>
/// <param name="prec"></param>
/// <returns></returns>
public static string PtToStr(Point3d pt, DistanceUnitFormat unitType, int prec)
{
return string.Format("({0}, {1}, {2})",
Converter.DistanceToString(pt.X, unitType, prec),
Converter.DistanceToString(pt.Y, unitType, prec),
Converter.DistanceToString(pt.Z, unitType, prec));
}
//
//--------
public static void Rotate(Entity ent, Point3d basePoint, double rotationAngle)
{
Rotate(ent.ObjectId, basePoint, rotationAngle);
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="basePoint"></param>
/// <param name="rotationAngleInRadians"></param>
public static void Rotate(ObjectId id, Point3d basePoint, double rotationAngleInRadians)
{
// note: System.Math.PI is const 3.1415926535897931
// struct Autodesk.AutoCAD.Geometry.Matrix3d
// struct Autodesk.AutoCAD.Geometry.Vector3d
// Matrix3d transform = Matrix3d.Rotation(((rotationAngleInDegrees * Math.PI) / 180,
//
Matrix3d transform = Matrix3d.Rotation(
rotationAngleInRadians,
new Vector3d(0, 0, 1),
basePoint);
using (Transaction tr =
AcadApp.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction())
{
((Entity)tr.GetObject(id, OpenMode.ForWrite, true)).TransformBy(transform);
tr.Commit();
}
}
//--------
//--------
public static void Move(Entity ent, Point3d fromPoint, Point3d toPoint)
{
Move(ent.ObjectId, fromPoint, toPoint);
}
//--------
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="fromPoint"></param>
/// <param name="toPoint"></param>
public static void Move(ObjectId id, Point3d fromPoint, Point3d toPoint)
{
Matrix3d transform = Matrix3d.Displacement(fromPoint.GetVectorTo(toPoint));
using (Transaction tr =
AcadApp.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction())
{
((Entity)tr.GetObject(id, OpenMode.ForWrite, true)).TransformBy(transform);
tr.Commit();
}
}
//--------
//--------
public static bool SelectPoint01(string Message, out Point3d pt )
{
return SelectPoint01(Message, out pt, new Point3d(), false );
}
/// <summary>
///
/// </summary>
/// <param name="Message"></param>
/// <param name="pt"></param>
/// <param name="defVal"></param>
/// <param name="useDefVal"></param>
/// <returns></returns>
public static bool SelectPoint01(string Message, out Point3d pt,Point3d defVal, bool useDefVal )
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
bool result = false;
PromptPointOptions ppo = new PromptPointOptions(Message);
if (useDefVal)
{
ppo.BasePoint = defVal;
ppo.UseDashedLine = true;
ppo.UseBasePoint = true;
ppo.AllowNone = true;
}
PromptPointResult ppr = ed.GetPoint(ppo);
switch (ppr.Status)
{
case PromptStatus.Cancel:
ed.WriteMessage("\nUser canceled.");
break ;
case PromptStatus.Error:
ed.WriteMessage("\nUser input error.");
break;
case PromptStatus.OK:
result = true;
break;
}
pt = ppr.Value;
return result;
}
//--------
//--------
[CommandMethod("TestPoints")]
public static void TestPoints()
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
Point3d pt = new Point3d(0, 0, 0);
bool result = KdubUtils.Utility.SelectPoint01("Select a Point ", out pt);
if (result)
{
MessageBox.Show("TestPoint_01 Result :" + KdubUtils.Utility.PtToStr(pt));
}
bool result2 = KdubUtils.Utility.SelectPoint01("Select a Point ", out pt, pt, true);
if (result2)
{
MessageBox.Show("TestPoint_02 Result :" + KdubUtils.Utility.PtToStr(pt));
}
}
[CommandMethod("TestMove")]
public static void TestMove()
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
PromptEntityResult peResult = ed.GetEntity("\nSelect an entity to Move: ");
Point3d pt1 = ed.GetPoint("\nMove From: ").Value;
Point3d pt2 = ed.GetPoint("\nMove To: ").Value;
KdubUtils.Utility.Move(peResult.ObjectId, pt1, pt2);
}
//------------
[CommandMethod("TestRotate")]
public static void TestRotate()
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
PromptEntityResult peResult = ed.GetEntity("\nSelect an entity to Rotate: ");
Point3d pt1 = ed.GetPoint("\nRotate From: ").Value;
PromptAngleOptions paOptions = new PromptAngleOptions("\nEnter or Select an Angle: ");
paOptions.AllowZero = true;
paOptions.AllowNone = false;
paOptions.DefaultValue = Math.PI * 45.0 / 180; // in Radians
paOptions.UseDefaultValue = true;
paOptions.BasePoint = pt1;
paOptions.UseBasePoint = true;
double ang1 = ed.GetAngle(paOptions).Value;
// MessageBox.Show("Angle is :" + Converter.AngleToString(ang1));
// ang1 is expressed in radians
KdubUtils.Utility.Rotate(peResult.ObjectId, pt1, ang1);
}
Bryco,
To come back to the Overloading ..
Here's a couple of examples of Method Overloading you may find interesting.
// class Autodesk.AutoCAD.Runtime.Converter
// enum Autodesk.AutoCAD.Runtime.DistanceUnitFormat
// struct Autodesk.AutoCAD.Geometry.Point3d
//--------
public static string PtToStr(Point2d pt)
{
return PtToStr(pt, DistanceUnitFormat.Current, -1);
}
//--------
public static void Rotate(Entity ent, Point3d basePoint, double rotationAngle)
{
Rotate(ent.ObjectId, basePoint, rotationAngle);
}
public static void Rotate(ObjectId id, Point3d basePoint, double rotationAngleInRadians)
{
Matrix3d transform = Matrix3d.Rotation(
rotationAngleInRadians,
new Vector3d(0, 0, 1),
basePoint);
using (Transaction tr =
AcadApp.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction())
{
((Entity)tr.GetObject(id, OpenMode.ForWrite, true)).TransformBy(transform);
tr.Commit();
}
}
// for one
public static void Rotate(ObjectId id, Point3d basePoint, double rotationAngleInRadians)
{
Rotate( new ObjectId[] { id }, basePoint, rotationAngleInRadians);
}
// for many
public static void Rotate(ObjectId[] ids, Point3d basePoint, double rotationAngleInRadians)
{
Matrix3d transform = Matrix3d.Rotation(
rotationAngleInRadians,
new Vector3d(0, 0, 1),
basePoint);
Database db = ids[0].Database;
using(Transaction tr = db.TransactionManager.StartTransaction())
{
foreach( ObjectId id in ids )
{
Entity ent = tr.GetObject(id, OpenMode.ForWrite) as Entity;
if( ent != null )
ent.TransformBy(transform);
}
tr.Commit();
}
}