TheSwamp
Code Red => .NET => Topic started by: Glenn R 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:
// 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.
-
Ohhh, Is there a prize for guessing correctly ?
Just a min' ...
-
Here 'tis ..
// note: opened in Debug mode
// with break for LoaderLock exception toggled OFF. kwb
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!
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")]
-
What are you after Glenn ?
This probably wont help, but I'm playing, so ...
[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();
}
}
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:
-
Looks like I have some serious reading to do .. XrefGraph, XrefGraphNode .. etc
// 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());
-
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........
-
VS2005 Pro with AC2006.
-
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.
-
Another example. I think you'll see the potential of this one?
[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,
-
Had a quick look ... but the cricket is on , so ...
-
Wow! That is awsome Glenn.
I may have some serious use for this.
How DO you find this stuff?????
-
Find? I study a lot and I write it...simple 8-)
It does help coming from ARX though.
-
BTW, that second example I posted, is a port of some code I did in ARX a couple of years back to do................binding.
-
Glad you liked it Draftek.
-
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