Author Topic: Xrefs and .NET 2.0  (Read 9102 times)

0 Members and 1 Guest are viewing this topic.

Glenn R

  • Guest
Xrefs and .NET 2.0
« on: January 09, 2006, 01:02:55 AM »
Lads & Lasses,

Can anybody give the code below a whirl in .NET 2.0 for AutoCAD (acad.exe.config must be using the latest runtime)
and post your results:

Code: [Select]
// Get a pointer to the current document...
Document doc = acadApp.DocumentManager.MdiActiveDocument;
// Get the current document's database...
Database db = doc.Database;
// Get a pointer to the current document's editor while we're at it...
Editor ed = doc.Editor;

ed.WriteMessage("\nAbout to try and get the xref graph...");
// Get the xref graph for the current database...
XrefGraph xrefGraph = db.GetHostDwgXrefGraph(false);
ed.WriteMessage("\nSuccessfully retrieved the xref graph!");

// Do we have any?
if (xrefGraph.NumNodes == 1) {
ed.WriteMessage("\nThere are no xrefs in the current drawing!");
return;
}

Try it in a drawing that has NO xrefs first, then attach an xref and try again.

Cheers,
Glenn.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Xrefs and .NET 2.0
« Reply #1 on: January 09, 2006, 02:06:45 AM »
Ohhh, Is there a prize for guessing correctly ?

Just a min' ...
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: Xrefs and .NET 2.0
« Reply #2 on: January 09, 2006, 02:20:37 AM »
Here 'tis ..
// note: opened in Debug mode
// with  break for LoaderLock exception toggled OFF. kwb
Quote
Command: netload

Command: testxx

About to try and get the xref graph...
Successfully retrieved the xref graph!
There are no xrefs in the current drawing!
Command: xref

Attach Xref "CTT": C:\CTT.dwg
"CTT" loaded.

Specify insertion point or [Scale/X/Y/Z/Rotate/PScale/PX/PY/PZ/PRotate]:
Command: Testxx

About to try and get the xref graph...
Successfully retrieved the xref graph!
Command: XREF

Overlay Xref "rope-sheave-dog": C:\rope-sheave-dog.dwg
"rope-sheave-dog" loaded.

Specify insertion point or [Scale/X/Y/Z/Rotate/PScale/PX/PY/PZ/PRotate]:
Command: Testxx


About to try and get the xref graph...
Successfully retrieved the xref graph!
Quote
With
Assembly System.Configuration
    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Configuration.dll

Attributes:
[System.CLSCompliantAttribute(true),
System.Diagnostics.DebuggableAttribute(2),
System.Security.AllowPartiallyTrustedCallersAttribute,
System.Runtime.InteropServices.ComVisibleAttribute(false),
System.Runtime.CompilerServices.CompilationRelaxationsAttribute(8),
System.Reflection.AssemblyInformationalVersionAttribute("2.0.50727.42"),
System.Reflection.AssemblyTitleAttribute("System.Configuration.dll"),
System.Runtime.InteropServices.ComCompatibleVersionAttribute(1, 0, 3300, 0),
System.Reflection.AssemblyKeyFileAttribute("f:\RTM\Tools\devdiv\FinalPublicKey.snk"),
System.Reflection.AssemblyDelaySignAttribute(true),
System.Resources.NeutralResourcesLanguageAttribute("en-US"),
System.Resources.SatelliteContractVersionAttribute("2.0.0.0"),
System.Runtime.CompilerServices.RuntimeCompatibilityAttribute,
System.Reflection.AssemblyFileVersionAttribute("2.0.50727.42"),
System.Reflection.AssemblyCopyrightAttribute("© Microsoft Corporation.  All rights reserved."),
System.Reflection.AssemblyProductAttribute("Microsoft® .NET Framework"),
System.Reflection.AssemblyCompanyAttribute("Microsoft Corporation"),
System.Reflection.AssemblyDefaultAliasAttribute("System.Configuration.dll"),
System.Reflection.AssemblyDescriptionAttribute("System.Configuration.dll")]
« Last Edit: January 09, 2006, 02:25:42 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: Xrefs and .NET 2.0
« Reply #3 on: January 09, 2006, 03:38:56 AM »
What are you after Glenn ?

This probably wont help, but I'm playing, so ...

Code: [Select]
        [CommandMethod("ListXrefs")]
        public static void ListXrefs()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Document doc = AcadApplication.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

            using (Transaction tr = db.TransactionManager.StartTransaction()) {
                BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
                foreach (ObjectId objId in btr) {
                    DBObject dbObj = tr.GetObject(objId, OpenMode.ForRead);
                    BlockReference blkRef = dbObj as BlockReference;
                    if (blkRef != null) {
                        BlockTableRecord blkDef = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);
                        if (blkDef.IsFromExternalReference == true) {
                            ed.WriteMessage("\nFound an XREF referencing : " + blkDef.Name);
                        }
                    }
                }               
                // Not needed 'cause the tr OpenMode is ForRead
                tr.Commit();
            }
        }

Quote
Opening an AutoCAD 2004 format file.

Resolve Xref "CTT": C:\CTT.dwg
"CTT" loaded.

Resolve Xref "rope-sheave-dog": C:\rope-sheave-dog.dwg
"rope-sheave-dog" loaded.
Regenerating model.

AutoCAD Express Tools Copyright © 2002-2004 Autodesk, Inc.

AutoCAD menu utilities loaded.
Command: netload

Command: LISTXREFs

Found an XREF referencing : CTT
Found an XREF referencing : rope-sheave-dog
Command:
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: Xrefs and .NET 2.0
« Reply #4 on: January 09, 2006, 03:54:49 AM »
Looks like I have some serious reading to do .. XrefGraph, XrefGraphNode .. etc
Code: [Select]
            // Do we have any?
            if (xrefGraph.NumNodes == 1) {
                ed.WriteMessage("\nThere are no xrefs in the current drawing!");
                return;
            }
            ed.WriteMessage("\nNodes : " + xrefGraph.NumNodes.ToString());

            XrefGraphNode xrefNode = xrefGraph.GetXrefNode(db);
            ed.WriteMessage("\nNode Status : " + xrefNode.XrefStatus.ToString());
            ed.WriteMessage("\nNode Name : " + xrefNode.Name.ToString());
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: Xrefs and .NET 2.0
« Reply #5 on: January 09, 2006, 04:19:47 AM »
Thanks for trying Kerry.

Are you using VS 2005 to build? I was using 2003 with acad.exe.config commented so it would use latest runtime (2.0 on my machine) and as soon as I would run it - boom - fatal errorski! It would not get to the second print statment...so it was failing on the xrefgraph.
I am using Map 2006 as well.

I'll post some more xref code in a wee while........

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Xrefs and .NET 2.0
« Reply #6 on: January 09, 2006, 04:31:35 AM »
VS2005 Pro  with AC2006.

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: Xrefs and .NET 2.0
« Reply #7 on: January 09, 2006, 05:28:39 AM »
Kerry,

Here is a project I was fiddling around with. I converted to VC Express and it ran smoothly.
Looks like I will have to convert over my entire code base to be safe.

I'll post another XREF example in a tick...need a bourbon  :-D

Cheers,
Glenn.
« Last Edit: January 09, 2006, 05:50:15 AM by Glenn R »

Glenn R

  • Guest
Re: Xrefs and .NET 2.0
« Reply #8 on: January 09, 2006, 05:38:13 AM »
Another example. I think you'll see the potential of this one?
Code: [Select]
[CommandMethod("NXL")]
static public void NestedXrefsCommand()
{
// Get a pointer to the active document...
Document pCurDoc = pAcadApp.DocumentManager.MdiActiveDocument;
// From the active document, get a pointer to the doc's dbase...
Database pCurDb = pCurDoc.Database;
// Get a pointer to the editor...
Editor pEd = pCurDoc.Editor;

// Get the xref graph for the current dbase...
XrefGraph pXrefGraph = pCurDb.GetHostDwgXrefGraph(true);
if (pXrefGraph.IsEmpty || pXrefGraph.NumNodes == 1)
{
pEd.WriteMessage("\nNo xrefs found.");
return; // No xrefs...
}

using (Transaction pTr = pCurDoc.TransactionManager.StartTransaction())
{
BlockTable pBlkTbl = (BlockTable)pTr.GetObject(pCurDb.BlockTableId, OpenMode.ForRead);

for (int i = 0; i < pXrefGraph.NumNodes; i++)
{
XrefGraphNode pXrefGraphNode = (XrefGraphNode)pXrefGraph.GetXrefNode(i);
// Is it the root node, which is the current drawing?
// If it IS, then the number of incoming nodes will be 0...
if (pXrefGraphNode.NumIn == 0)
continue;
// Continue if it's truly nested...
if (pXrefGraphNode.IsNested)
continue;
// Number of incoming nodes should only be 1 (the current drawing itself)
// for TRUE top level non-nested xref...
if (pXrefGraphNode.NumIn > 1)
continue;

pEd.WriteMessage("\nXref name: " + pXrefGraphNode.Name);
pEd.WriteMessage("\nNested status: " + pXrefGraphNode.IsNested.ToString());
pEd.WriteMessage("\nIncoming nodes: " + pXrefGraphNode.NumIn.ToString());
pEd.WriteMessage("\nOutgoing nodes: " + pXrefGraphNode.NumOut.ToString());

}//for

// Commit the transaction on the dbase...
pTr.Commit();
}//using

}

Cheers,

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Xrefs and .NET 2.0
« Reply #9 on: January 09, 2006, 06:00:14 AM »
Had a quick look ... but the cricket is on ,  so ...
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.

Draftek

  • Guest
Re: Xrefs and .NET 2.0
« Reply #10 on: January 09, 2006, 07:58:33 AM »
Wow! That is awsome Glenn.

I may have some serious use for this.

How DO you find this stuff?????

Glenn R

  • Guest
Re: Xrefs and .NET 2.0
« Reply #11 on: January 09, 2006, 08:15:54 AM »
Find? I study a lot and I write it...simple  8-)
It does help coming from ARX though.

Glenn R

  • Guest
Re: Xrefs and .NET 2.0
« Reply #12 on: January 09, 2006, 08:17:52 AM »
BTW, that second example I posted, is a port of some code I did in ARX a couple of years back to do................binding.

Glenn R

  • Guest
Re: Xrefs and .NET 2.0
« Reply #13 on: January 09, 2006, 08:18:30 AM »
Glad you liked it Draftek.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Xrefs and .NET 2.0
« Reply #14 on: January 09, 2006, 08:25:48 AM »
Compiled, loaded and ran <from the Zip stuff I mean> ... I just need to build some nested xrefs to work with.
.. though I thought I had some from the last time we played with this :-)

Tucked away for future study, thanks Glenn
« Last Edit: January 09, 2006, 08:41:50 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.