Author Topic: xref  (Read 4468 times)

0 Members and 1 Guest are viewing this topic.

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 »
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

--> Donate to theSwamp<--

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.

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

--> Donate to theSwamp<--

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!

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

--> Donate to theSwamp<--

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6979
  • 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 ! 

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

--> Donate to theSwamp<--

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