class test
{
const int RTNONE = 5000; /* No result */
const int RTREAL = 5001; /*Real number */
const int RTPOINT = 5002; /* 2D point X and Y only */
const int RTSHORT = 5003; /* Short integer */
const int RTANG = 5004; /* Angle */
const int RTSTR = 5005; /* String */
const int RTENAME = 5006; /* Entity name */
const int RTPICKS = 5007; /* Pick set */
const int RTORINT = 5008; /* Orientation */
const int RT3DPOINT = 5009; /* 3D point - X, Y, and Z */
const int RTLONG = 5010; /* Long integer */
const int RTVOID = 5014; /* Blank symbol */
const int RTLB = 5016; /* list begin */
const int RTLE = 5017; /* list end */
const int RTDOTE = 5018; /* dotted pair */
const int RTNIL = 5019; /* nil */
const int RTDXF0 = 5020; /* DXF code 0 for ads_buildlist only */
const int RTT = 5021; /* T atom */
const int RTRESBUF = 5023; /* resbuf */
const int RTMODELESS = 5027; /* interrupted by modeless dialog */
//http://forums.autodesk.com/t5/NET/acedcommand-function-in-Net-managed-classes/m-p/1332900
//TonyT
//Requires Allow Unsafe code to be checked in Build
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "acedCmd")]
extern static int acedCmd(IntPtr pResbuf);
unsafe static int Command(ResultBuffer args)
{
if (!acadApp.DocumentManager.IsApplicationContext)
return acedCmd((IntPtr)args.UnmanagedObject.ToPointer());
else
return 0;
}
[CommandMethod("Flatshot3Views")]
public static void Flatshot3Views()
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
acadApp.SetSystemVariable("TILEMODE", 1);
string[] sName = new string[] { "Top", "Front", "Left" };
ObjectIdCollection ViewIds = new ObjectIdCollection();
string cLayer = (string)acadApp.GetSystemVariable("CLAYER");
ViewTableRecord CurrentVtr = ed.GetCurrentView();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
bool hasSolid = false;
RXClass rxSolid = RXClass.GetClass(typeof(Solid3d));
ViewTable vt = tr.GetObject(db.ViewTableId, OpenMode.ForWrite) as ViewTable;
ViewTableRecord vtr;
//Make the views
for (int i = 0; i < 3; i++)
{
string name = sName[i];
if (vt.Has(name))
ViewIds.Add(vt[name]);
else
{
OrthographicView ov;
vtr = new ViewTableRecord();
vtr.Name = name;
if (i == 0)
ov = OrthographicView.TopView;
else if (i == 1)
ov = OrthographicView.FrontView;
else
ov = OrthographicView.LeftView;
vtr.SetViewDirection(ov);
vtr.SetUcs(ov);
ViewIds.Add(vt.Add(vtr));
tr.AddNewlyCreatedDBObject(vtr, true);
}
}
tr.Commit();
}
//Get the insertion point
PromptPointOptions ppo = new PromptPointOptions("\nPick the insertion point:");
PromptPointResult per = ed.GetPoint(ppo);
if (per.Status != PromptStatus.OK) return;
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
Point3d p = per.Value.TransformBy(ucs);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
for (int i = 0; i < 3; i++)
{
string name = sName[i];
ed.SetCurrentView((ViewTableRecord)tr.GetObject(ViewIds[i], OpenMode.ForRead));
ResultBuffer args = new ResultBuffer();
args.Add(new TypedValue(RTSTR, "Flatshot"));
args.Add(new TypedValue(RT3DPOINT, p));
args.Add(new TypedValue(5001, 1.0));
args.Add(new TypedValue(5001, 1.0));
args.Add(new TypedValue(5001, 0.0));
try
{
ed.WriteMessage(Environment.NewLine + "Command=" + Command(args).ToString());
}
catch (System.Exception ex)
{
//ed.WriteMessage(Environment.NewLine + ex);
throw;
}
//com doesn't work
//((com)acadApp.AcadApplication).ActiveDocument.SendCommand("Flatshot " + sPoint +" " );
ObjectId brefId = SelectLastEnt(doc);
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
if (brefId != ObjectId.Null)
{
BlockReference br = tr.GetObject(brefId, OpenMode.ForWrite) as BlockReference;
if (br == null) continue;
BlockTableRecord btr = tr.GetObject(br.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;
string bname = name;
if (!bt.Has(name))
btr.Name = name;
else
{
int j = 1;
while (true)
{
bname = name + j.ToString();
if (!bt.Has(bname))
{
btr.Name = bname;
break;
}
j++;
}
}
}
}
tr.Commit();
}
ed.SetCurrentView(CurrentVtr);
} //end Flatshot3Views
//Kerry
static public ObjectId SelectLastEnt(Document doc)
{
PromptSelectionResult LastEnt = doc.Editor.SelectLast();
if (LastEnt.Value != null && LastEnt.Value.Count == 1)
{
return LastEnt.Value[0].ObjectId;
}
return ObjectId.Null;
}
} //endclass test
[assembly: CommandClass(typeof(ClassLibrary4.test))]
using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Internal;
using Autodesk.AutoCAD.Colors;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly:CommandClass(typeof(Testing.test))]
namespace Testing
{
class test
{
const int RTREAL = 5001; /*Real number */
const int RTSTR = 5005; /* String */
const int RT3DPOINT = 5009; /* 3D point - X, Y, and Z */
//http://forums.autodesk.com/t5/NET/acedcommand-function-in-Net-managed-classes/m-p/1332900
//TonyT
//Requires Allow Unsafe code to be checked in Build
[DllImport("acad.exe", CallingConvention = CallingConvention.Cdecl,
EntryPoint = "acedCmd")]
extern static int acedCmd(IntPtr pResbuf);
unsafe static int Command(ResultBuffer args)
{
if (!acadApp.DocumentManager.IsApplicationContext)
return acedCmd((IntPtr)args.UnmanagedObject.ToPointer());
else
return 0;
}
[CommandMethod("Flatshot3Views")]
public static void Flatshot3Views()
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
acadApp.SetSystemVariable("TILEMODE", 1);
string[] sName = new string[] { "Top", "Front", "Left" };
ObjectIdCollection ViewIds = new ObjectIdCollection();
string cLayer = (string)acadApp.GetSystemVariable("CLAYER");
ViewTableRecord CurrentVtr = ed.GetCurrentView();
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
bool hasSolid = false;
RXClass rxSolid = RXClass.GetClass(typeof(Solid3d));
//Check for no solids
foreach (ObjectId blockId in bt)
{
foreach (ObjectId id in (BlockTableRecord)tr.GetObject(blockId, OpenMode.ForRead))
{
if (id.ObjectClass.IsDerivedFrom(rxSolid))
{
hasSolid = true;
break;
}
}
if (!hasSolid)
{
System.Windows.Forms.MessageBox.Show("There are no solids.");
return;
}
}
ViewTable vt = tr.GetObject(db.ViewTableId, OpenMode.ForWrite) as ViewTable;
ViewTableRecord vtr;
//Make the views
for (int i = 0; i < 3; i++)
{
string name = sName[i];
if (vt.Has(name))
ViewIds.Add(vt[name]);
else
{
OrthographicView ov;
vtr = new ViewTableRecord();
vtr.Name = name;
if (i == 0)
ov = OrthographicView.TopView;
else if (i == 1)
ov = OrthographicView.FrontView;
else
ov = OrthographicView.LeftView;
vtr.SetViewDirection(ov);
vtr.SetUcs(ov);
ViewIds.Add(vt.Add(vtr));
tr.AddNewlyCreatedDBObject(vtr, true);
}
}
tr.Commit();
}
//Get the insertion point
PromptPointOptions ppo = new PromptPointOptions("\nPick the insertion point:");
PromptPointResult per = ed.GetPoint(ppo);
if (per.Status != PromptStatus.OK) return;
Matrix3d ucs = ed.CurrentUserCoordinateSystem;
Point3d p = per.Value.TransformBy(ucs);
ed.WriteMessage(Environment.NewLine + "Set the Foreground Lines color field to bylayer" + Environment.NewLine);
//Add layers
AddLayer("Top", 200, "Continuous");
AddLayer("Front", 205, "Continuous");
AddLayer("Left", 210, "Continuous");
AddLayer("Top-Hidden", 5, "HIDDEN2");
AddLayer("Front-Hidden", 5, "HIDDEN2");
AddLayer("Left-Hidden", 5, "HIDDEN2");
ResultBuffer args = new ResultBuffer();
args.Add(new TypedValue(RTSTR, "Flatshot"));
args.Add(new TypedValue(RT3DPOINT, p));
args.Add(new TypedValue(5001, 1.0));
args.Add(new TypedValue(5001, 1.0));
args.Add(new TypedValue(5001, 0.0));
using (Transaction tr = db.TransactionManager.StartTransaction())
{
for (int i = 0; i < 3; i++)
{
string name = sName[i];
acadApp.SetSystemVariable("CLAYER",name);
ed.SetCurrentView((ViewTableRecord)tr.GetObject(ViewIds[i], OpenMode.ForRead));
try
{
Command(args);
}
catch (System.Exception ex)
{
ed.WriteMessage(Environment.NewLine + ex);
throw;
}
//((com)acadApp.AcadApplication).ActiveDocument.SendCommand("Flatshot " + sPoint +" " );
ObjectId brefId = Utils.EntLast();
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
if (brefId != ObjectId.Null)
{
BlockReference br = tr.GetObject(brefId, OpenMode.ForWrite) as BlockReference;
if (br == null) continue;
BlockTableRecord btr = tr.GetObject(br.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;
string bname = name;
if (!bt.Has(name))
btr.Name = name;
else
{
int j = 1;
while (true)
{
bname = name + j.ToString();
if (!bt.Has(bname))
{
btr.Name = bname;
break;
}
j++;
}
}
foreach (ObjectId entId in btr)
{
Entity ent = tr.GetObject(entId, OpenMode.ForWrite) as Entity;
if (ent.ColorIndex == 0)
{
ent.Layer = sName[i] + "-Hidden";
ent.ColorIndex = 256;
ent.Linetype = "ByLayer";
}
}
}
}
tr.Commit();
}
acadApp.SetSystemVariable("CLAYER",cLayer);
ed.SetCurrentView(CurrentVtr);
}
catch (System.Exception ex)
{
ed.WriteMessage(Environment.NewLine + ex);
throw;
}
} //end Flatshot3Views
static private bool AddLayer(string sLayer, short iColor, string sLtype)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead, false) as LayerTable;
if (lt.Has(sLayer)) return true;
lt.UpgradeOpen();
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = sLayer;
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, iColor);
LinetypeTable linet = tr.GetObject(db.LinetypeTableId, OpenMode.ForRead, false) as LinetypeTable;
if (linet.Has(sLtype))
ltr.LinetypeObjectId = linet[sLtype];
else
{
string Acadlinetypes ="acadiso.lin";
AddLinetype(sLtype, Acadlinetypes,db);
}
lt.Add(ltr);
tr.AddNewlyCreatedDBObject(ltr, true);
tr.Commit();
return true;
}
} //end AddLayerCode
static public ObjectId AddLinetype(string sLType, string sFile, Database db)
{
ObjectId LTypeID = ObjectId.Null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
LinetypeTable LTypeTable = tr.GetObject
(db.LinetypeTableId, OpenMode.ForRead, false) as LinetypeTable;
if (LTypeTable.Has(sLType))
LTypeID = LTypeTable[sLType];
else
{
db.LoadLineTypeFile(sLType, sFile);
LTypeID = LTypeTable[sLType];
}
tr.Commit();
return LTypeID;
}
} //end AddLinetype
} //endclass test
}
2010 Express and cad 2012 won't run this at all without crashing. Must be less leeway with threading or something
(IntPtr)args.UnmanagedObject.ToPointer() AND args.UnmanagedObject print out as system.intPtr so I am lost on this one.
Perhaps the failure is due to a limitation of acedcmd (or flatshot command)
Kaeker, I tried it and it is not asynchronous.(If I got the right word) and the 3 commands execute after the code.
exmachina, thanks for trying but the code works fine up till acad 2008, crashes one time only per computer in 2009 to 2011 then works fine. 2012 hardly ever. So I think it is a net version issue.
TheMaster- I tried acedCmd( resbuf.UnmanagedObject ); but no difference.
I think it is the fact that the command has a gui that causes the problem.
I'm just experimenting w/ filedia=0;
Kaeker, I tried it and it is not asynchronous.(If I got the right word) and the 3 commands execute after the code.
Kaeker, I tried it and it is not asynchronous.(If I got the right word) and the 3 commands execute after the code.
Disagree: the exection of your code is stayed while SendCommand waits for completion of the dialog (it is synchronous, just what Kean said). The problem at least with my AutoCAD is that there's no command line version of the FLATSHOT command.
If I'm not mistaken, if a dialog temporarily hides itself to allow the user to supply graphical input (e.g, picking points), SendCommand() can not run synchronously.
string args = System.String.Format("_Flatshot {0},{1},{2} 1.0 1.0 0.0\n", p.X, p.Y, p.Z);
If I'm not mistaken, if a dialog temporarily hides itself to allow the user to supply graphical input (e.g, picking points), SendCommand() can not run synchronously.
Can acedCmd, apart from an indication that something went wrong?
Take this command string:Code: [Select]string args = System.String.Format("_Flatshot {0},{1},{2} 1.0 1.0 0.0\n", p.X, p.Y, p.Z);
Picking an existing block won't work. But feeding it to SendCommand and just clicking "Create" kind of does, in a way, perhaps.
kaefer apologies for spelling your name wrong in previous post, was typo