PDA

View Full Version : UCS



cadprog
2007-11-12, 06:09 AM
Can anybody tell me where I have gone wrong? As this code is intended to change the ucs after the XATTACH command ends, it does not change back to the previous ucs. Here is the code.


using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.Geometry;

using System;
using System.Collections.Specialized;
using System.Windows.Forms;

namespace XU2
{
public class XU:IExtensionApplication
{
public static Matrix3d ucs;

public void Initialize()
{
DocumentCollection dm = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
dm.DocumentLockModeChanged += new DocumentLockModeChangedEventHandler(Change);
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
doc.CommandEnded += new CommandEventHandler(doc_CommandEnded);

}

public void Terminate()
{
}

private void Change(object sender,DocumentLockModeChangedEventArgs e)
{
if (e.GlobalCommandName == "XATTACH")
{
ModifyLayers();
CreateLayer();
ucs = UCS2WCS();
}
}

private static ObjectId CreateLayer()
{
ObjectId layerId = ObjectId.Null;//the return value for this function
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

using (Transaction trans = db.TransactionManager.StartTransaction())
{
//Get the layer table first...
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForRead);

//Check if 0-XREF exists;;;
if (lt.Has("0-XREF"))
{
layerId = lt["0-XREF"];
db.Clayer = layerId;
}
else
{
//if not, create the layer here.
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "0-XREF";
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 1);
ltr.IsFrozen = false;
ltr.IsLocked = false;
ltr.IsOff = false;
//upgrade the open from read to write
lt.UpgradeOpen();
layerId = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true);
db.Clayer = layerId;
}
trans.Commit();
}
return layerId;
}

public void ModifyLayers()
{
Database db = HostApplicationServices.WorkingDatabase;

try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
foreach (ObjectId objId in lt)
{
LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(objId, OpenMode.ForWrite);

if (ltr.Name == "0" || ltr.Name == "Defpoints")
{
if (ltr.IsLocked) { ltr.IsLocked = false; }
if (ltr.IsFrozen) { ltr.IsFrozen = false; }
if (ltr.IsOff) { ltr.IsOff = false; }
}
}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}

}

public static Matrix3d UCS2WCS()
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

Matrix3d original = ed.CurrentUserCoordinateSystem;

ed.CurrentUserCoordinateSystem = Matrix3d.Identity;

return original;

}

void doc_CommandEnded(object sender, CommandEventArgs e)
{
try
{
if (e.GlobalCommandName == "XATTACH")
{
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\n The command is XATTACH");
ed.CurrentUserCoordinateSystem = ucs;
ed.WriteMessage("\n" + ucs);
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}

}
}

Thanks

Kerry Brown
2007-11-12, 09:32 AM
Have a look at your posting at theSwamp