@_gile wrote:
swaywood a écrit :
in autocad 2016 not work too
Since AutoCAD 2015 you have to use the Editor.Command() (or CommandAsync()) method instead of the wrapper of Editor.Runcommand() provided by tony Tanzillo.
See these threads:
http://through-the-interface.typepad.com/through_the_interface/2014/03/autocad-2015-calling-commands...
https://www.theswamp.org/index.php?topic=49124.msg542370#msg542370
Hi @_gile, regarding your swamp post:
The last shown behavior is interesting, for example, to pass a list of arguments to a command (e.g. a points list for LINE, PLINE, SPLINE).
This also requires the CommandAsync using.
public async void Cmd3()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Point2d[] pts =
{
new Point2d(0.0, -10.0),
new Point2d(10.0, 0.0),
new Point2d(0.0, 10.0),
new Point2d(-10.0, 0.0)
};
await ed.CommandAsync("_.PLINE");
foreach (Point2d pt in pts)
{
await ed.CommandAsync(pt);
}
await ed.CommandAsync("_close");
ZoomEntLast();
}
This is not true at all.
When a method takes a variable number of arguments of a specific type, you can pass individual arguments separated by commas, or you can pass all arguments in a single array of the same type as the params array.
[CommandMethod("COMMANDARGS")]
public static void GilesCommandExample()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Point2d[] points =
{
new Point2d(0.0, -10.0),
new Point2d(10.0, 0.0),
new Point2d(0.0, 10.0),
new Point2d(-10.0, 0.0)
};
object[] args =
new object[] {"._PLINE"}
.Concat(points.Cast<Object>())
.Concat(new [] {"", "._ZOOM", "_Extents"})
.ToArray();
// For any type 'T', if a method takes a 'params T[] args',
// you can pass individual arguments of type T, or you can
// pass all arguments contained in a single array of T[]:
ed.Command(args);
}
But we can make things a bit easier as well:
public static class MyCommandHelpers
{
public static object[] BuildList(this Editor editor, params object[] args)
{
return BuildListWorker(args).ToArray();
}
static IEnumerable<object> BuildListWorker(IEnumerable args)
{
if(args != null)
{
foreach(object o in args)
{
if(o != null)
{
IEnumerable items = o as IEnumerable;
if(items != null && !(items is string))
{
foreach(object item in BuildListWorker(items))
{
yield return item;
}
}
else
yield return o;
}
}
}
}
}
[CommandMethod("COMMANDARGS2")]
public static void CommandExample2()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
Point2d[] points =
{
new Point2d(0.0, -11.0),
new Point2d(11.0, 0.0),
new Point2d(0.0, 11.0),
new Point2d(-11.0, 0.0)
};
ed.Command(ed.BuildList("._PLINE", points, "", "._ZOOM", "_Extents"));
}
BuildList() works with any type of IEnumerable, for example:
[CommandMethod("CHANGETORED")]
public static void CommandArgs3()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
var psr = ed.GetSelection();
if(psr.Status == PromptStatus.OK)
{
ed.Command(ed.BuildList("._CHPROP", psr.Value.GetObjectIds(), "", "color", 1, ""));
}
}