Author Topic: xref  (Read 8157 times)

0 Members and 1 Guest are viewing this topic.

cadpro

  • Guest
xref
« 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

Bob Wahr

  • Guest
Re: xref
« Reply #1 on: November 08, 2007, 12:55:47 AM »
There's probably a way to do it programmatically, but if all else fails, you can send a ".xref" comand.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: xref
« Reply #2 on: November 08, 2007, 04:56:16 AM »
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
« Last Edit: November 08, 2007, 03:21:24 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Glenn R

  • Guest
Re: xref
« Reply #3 on: November 08, 2007, 07:45:50 AM »
< sigh >

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8661
  • AKA Daniel
Re: xref
« Reply #4 on: November 08, 2007, 07:47:39 AM »
(setq T nil)

Glenn R

  • Guest
Re: xref
« Reply #5 on: November 08, 2007, 07:49:18 AM »
 :evil:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: xref
« Reply #6 on: November 08, 2007, 03:04:10 PM »
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
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: xref
« Reply #7 on: November 08, 2007, 04:48:14 PM »

I've just noticed this post replicated at several other locations ..

cadpro  ,  are you having some sort of competition ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Bob Wahr

  • Guest
Re: xref
« Reply #8 on: November 08, 2007, 04:57:12 PM »
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.

Glenn R

  • Guest
Re: xref
« Reply #9 on: November 08, 2007, 07:18:03 PM »
Got it in one there me thinks Bob.

cadpro

  • Guest
Re: xref
« Reply #10 on: November 12, 2007, 01:18:15 AM »
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.

Code: [Select]
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

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: xref
« Reply #11 on: November 12, 2007, 04:03:43 AM »

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 ..


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: xref
« Reply #12 on: November 12, 2007, 04:17:44 AM »


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.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

cadpro

  • Guest
Re: xref
« Reply #13 on: November 12, 2007, 07:17:27 AM »
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

cadpro

  • Guest
Re: xref
« Reply #14 on: November 12, 2007, 07:36:39 AM »

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:

Code: [Select]
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);
            }
        }
« Last Edit: November 12, 2007, 07:54:46 AM by cadpro »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: xref
« Reply #15 on: November 12, 2007, 08:34:54 AM »
..................   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.

Quote
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 ..
« Last Edit: November 12, 2007, 08:46:38 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: xref
« Reply #16 on: November 12, 2007, 09:24:46 AM »

Third thoughts,
perhaps you should attach your complete solution.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

cadpro

  • Guest
Re: xref
« Reply #17 on: November 13, 2007, 12:39:58 AM »
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!


Quote
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

cadpro

  • Guest
Re: xref
« Reply #18 on: November 13, 2007, 03:19:07 AM »
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#.

Code: [Select]
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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: xref
« Reply #19 on: November 13, 2007, 04:28:21 AM »
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.



Quote
I'm posting the code, so that it may be of help to others who is just into C#.

Excellent idea!

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8661
  • AKA Daniel
Re: xref
« Reply #20 on: November 13, 2007, 08:07:08 AM »
Nice Coding Cadpro

PS: Kerry, would you please stop Crossposting  :-D
« Last Edit: November 13, 2007, 08:47:01 AM by Daniel »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: xref
« Reply #21 on: November 13, 2007, 06:51:34 PM »
Nice Coding Cadpro

PS: Kerry, would you please stop Crossposting  :-D

Da debil mad me doit ! 

:)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

cadpro

  • Guest
Re: xref
« Reply #22 on: November 14, 2007, 01:05:57 AM »

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