Thanks for trying it out Jeff.
I can't figure it out either, but with the following code (very little changed) I crashed 1 machine and 4 others didn't.
On the machine that crashed, reopening cad and running it again numerous times there were no more crashes.
So perhaps something gets set after the first try. (Very Scientific explanation lol).
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
}