TheSwamp
Code Red => .NET => Topic started by: cadpro on November 08, 2007, 12:48:07 AM
-
Hi all,
In C#, I have undefined the Xref command for some reason. After some coding, I need to bring back the Xref dialog when the user gives the XREF command. How do I bring back the xref dialog?
Thanks
-
There's probably a way to do it programmatically, but if all else fails, you can send a ".xref" comand.
-
Hi all,
In C#, I have undefined the Xref command for some reason................
If you don't know why, we won't be much help.
Have you also changed FILEDIA and CMDDIA and EXPERT
-
< sigh >
-
(setq T nil)
-
:evil:
-
I'm interested in seeing the code ..
in the mean-time, you could try something like any of these at the commandline :
.xref
externalreferences
.externalreferences
Command: redefine
Enter command name: xref
-
I've just noticed this post replicated at several other locations ..
cadpro , are you having some sort of competition ??
-
Judging by his previous posts here, he is looking for the quickest solution and by solution I mean completed code all shiny and wrapped up with a big red bow on it. Unfortunately, unless he was a bit more eloquent, and by a bit I mean incredibly huge amounts, on the other forums, it's pretty close to impossible to know exactly what he wants, and absolutely impossible to know what he has done.
Maybe I'm just being cynical but it looks like he would rather just order fish sticks.
-
Got it in one there me thinks Bob.
-
My apologies to all. As I'm a novice, and due to my interest in learning C#, I did post a few questions. Anyways, I've done something of my own with a few references here and there. But unfortunately, it doesn't change the UCS back to the previous one after the command ends. Can anybody tell me where I have gone wrong? 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
-
works for me
Command: xattach
Attach Xref "Test": C:\Test.dwg
"Test" loaded.
The command is XATTACH
((0.822724527413075,-0.568440280058454,0,840.784370459755),(0.568440280058454,0.
822724527413075,0,643.446602615863),(0,0,1,0),(0,0,0,1))
also,
you may want to restore the layer to the previous setting ..
-
Cadpro
Just a note :
The original query does not mention event trapping, it mentions redefined commands.
You could help yourself a little better if the query was relevant.
... also posting a working (or failing) sample of code may also give a better indication of the direction you are trying to take.
-
Thanks for the reply kerry.
Yes, I started coding this work with undefining and redefining xref. But as I went on furthur coding, I changed the whole logic. Don't know if I'm right. As I mentioned in the earlier post, I'm just a novice. Thought I would go about checking the e.CommandName in code to see if xref or xattach, whichever is correct, is typed by the user.
And, btw, I don't know why it is not working for me. Can it be something wrong with the files I'm testing with. I will attach the files too.
Thanks
-
also,
you may want to restore the layer to the previous setting ..
Yes, I think that is a good suggession. I would want to bring back the previous layer. Hence I globally declared a variable like this:
public static ObjectId curLayer;
void doc_CommandEnded(object sender, CommandEventArgs e)
{
try
{
if (e.GlobalCommandName == "XATTACH")
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\n The command is XATTACH");
ed.CurrentUserCoordinateSystem = ucs;
ed.WriteMessage("\n" + ucs);
db.Clayer = curLayer;
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
-
.................. Can it be something wrong with the files I'm testing with. I will attach the files too.
Still works for me with your drawings.
Loading AEC Dimensions Base...
Opening an AutoCAD 2004/LT 2004 format file.
Regenerating model.
I'm using AC2008, your drawings are saved as 2004. What Acad version are you using ??
and a piccy ..
-
Third thoughts,
perhaps you should attach your complete solution.
-
Ah yes! Now I understand how it works. It worked when I did the following steps.
1. Open AutoCAD.
2. Open the sample a.dwg.
3. Netload .dll
4. Type XATTACH. It works!
As far as I understood, all works with the above steps, except the CommandEnded Event.
The following steps doesn't make it to work.
1. Open AutoCAD.
2. Netload .dll
3. Open the sample a.dwg.
4. Type XATTACH. It doesn't work!
What Acad version are you using ??
Even though the drawing was created in Acad 2006, I've referenced Acad 2008 dlls, which I don't think is a problem.
Thanks
-
Kerry, I got it to work with a few tips, I digged from Autodesk discussion forum. Thanks to javenkat!
I'm posting the code, so that it may be of help to others who is just into C#.
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 static ObjectId curLayer;
private Autodesk.AutoCAD.ApplicationServices.DocumentCollection docs = null;
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);
docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
foreach (Document doc in docs)
{
Attach(doc);
}
docs.DocumentCreated += new Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventHandler(r1);
docs.DocumentToBeDestroyed += new Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventHandler(r2);
}
public void Terminate()
{
docs.DocumentCreated -= new Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventHandler(r1);
docs.DocumentToBeDestroyed -= new Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventHandler(r2);
}
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;
curLayer = db.Clayer;
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")
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\n The command is " + e.GlobalCommandName);
ed.CurrentUserCoordinateSystem = ucs;
ed.WriteMessage("\n" + ucs);
db.Clayer = curLayer;
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void doc_CommandCancelled(object sender, CommandEventArgs e)
{
try
{
if (e.GlobalCommandName == "XATTACH" )
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
ed.CurrentUserCoordinateSystem = ucs;
db.Clayer = curLayer;
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void r1(object sender, Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs e)
{
Attach(e.Document);
}
void r2(object sender, Autodesk.AutoCAD.ApplicationServices.DocumentCollectionEventArgs e)
{
Detach(e.Document);
}
private void Attach(Document doc)
{
doc.CommandEnded +=new CommandEventHandler(doc_CommandEnded);
doc.CommandCancelled +=new CommandEventHandler(doc_CommandCancelled);
}
private void Detach(Document doc)
{
doc.CommandEnded -= new CommandEventHandler(doc_CommandEnded);
doc.CommandCancelled -= new CommandEventHandler(doc_CommandCancelled);
}
}
}
But if I run XATTACH through the XREF dialog box, the CommandEnded and CommandCancelled Event is not fired. Hence the layer and ucs doesn't go back to the original setting.
Thanks
-
Ah yes! Now I understand how it works. It worked when I did the following steps.
1. Open AutoCAD.
2. Open the sample a.dwg.
3. Netload .dll
4. Type XATTACH. It works!
Thanks
Good ! Yes, the events are document specific, and the triggers must be defined in each document.
I'm posting the code, so that it may be of help to others who is just into C#.
Excellent idea!
-
Nice Coding Cadpro
PS: Kerry, would you please stop Crossposting :-D
-
Nice Coding Cadpro
PS: Kerry, would you please stop Crossposting :-D
Da debil mad me doit !
:)
-
But if I run XATTACH through the XREF dialog box, the CommandEnded and CommandCancelled Event is not fired. Hence the layer and ucs doesn't go back to the original setting.
Any suggessions on how I can resolve this, please?
Thanks