Skip to content

Commit 11f3ee2

Browse files
committed
test
1 parent 1b8115d commit 11f3ee2

File tree

6 files changed

+203
-7
lines changed

6 files changed

+203
-7
lines changed

Test/ExtractFileFromResources.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Autodesk.AutoCAD.DatabaseServices;
99
using Autodesk.AutoCAD.Geometry;
1010
using Autodesk.AutoCAD.Runtime;
11+
using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
1112
using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
1213
namespace Test;
1314

@@ -36,7 +37,7 @@ public void GetResource()
3637
[CommandMethod("ExtractFile")]
3738
public static void RunDocumentCommand()
3839
{
39-
var dwg = CadApp.DocumentManager.MdiActiveDocument;
40+
var dwg = Application.DocumentManager.MdiActiveDocument;
4041
var ed = dwg.Editor;
4142

4243
try
@@ -46,7 +47,7 @@ public static void RunDocumentCommand()
4647
{
4748
//Use this drawing file from AutoCAD's temp file location
4849
// such as insert this block file into current drawing
49-
CadApp.ShowAlertDialog(
50+
Application.ShowAlertDialog(
5051
"Block file saved in project resources has been extracted!");
5152

5253
File.Delete(blkFile);
@@ -69,8 +70,8 @@ public static string ExtractFileFromResource(string resourceName)
6970
(string)preferences.Files.TempFilePath + "\\" + resourceName + ".dwg";
7071
try
7172
{
72-
ResourceManager ResourceManager = new ResourceManager("Test.g.resources", Assembly.GetExecutingAssembly());
73-
var bytes = (byte[])ResourceManager.GetObject(resourceName);
73+
ResourceManager resourceManager = new ResourceManager("Test.g.resources", Assembly.GetExecutingAssembly());
74+
byte[]? bytes = (byte[])resourceManager.GetObject(resourceName);
7475
if (File.Exists(filePathName)) File.Delete(filePathName);
7576
using (var stream = new FileStream(filePathName, FileMode.Create, FileAccess.Write))
7677
{
@@ -112,9 +113,9 @@ private static ObjectId InsertBlock(string path, Point3d loc)
112113
BlockReference ent = new BlockReference(loc, btr.ObjectId);
113114
ent.ScaleFactors = scl;
114115

115-
BlockTableRecord modelspace =
116+
BlockTableRecord? modelspace =
116117
tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
117-
modelspace.AppendEntity(ent);
118+
modelspace!.AppendEntity(ent);
118119
tr.AddNewlyCreatedDBObject(ent, true);
119120

120121
retId = ent.ObjectId;

Test/LayoutTest.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Autodesk.AutoCAD.ApplicationServices;
2+
using Autodesk.AutoCAD.DatabaseServices;
3+
using Autodesk.AutoCAD.Runtime;
4+
using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
5+
6+
namespace Test;
7+
8+
public class LayoutTest
9+
{
10+
[CommandMethod("ListLayouts")]
11+
public void ListLayouts()
12+
{
13+
// Get the current document and database
14+
Document acDoc = Application.DocumentManager.MdiActiveDocument;
15+
Database acCurDb = acDoc.Database;
16+
17+
// Get the layout dictionary of the current database
18+
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
19+
{
20+
DBDictionary? lays =
21+
acTrans.GetObject(acCurDb.LayoutDictionaryId,
22+
OpenMode.ForRead) as DBDictionary;
23+
24+
acDoc.Editor.WriteMessage("\nLayouts:");
25+
26+
// Step through and list each named layout and Model
27+
foreach (DBDictionaryEntry item in lays)
28+
{
29+
acDoc.Editor.WriteMessage("\n " + item.Key);
30+
}
31+
32+
// Abort the changes to the database
33+
acTrans.Abort();
34+
}
35+
}
36+
}

Test/Test.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,10 @@
5656
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
5757
</Resource>
5858
</ItemGroup>
59+
<ItemGroup>
60+
<Compile Update="ZoomToObject.cs">
61+
<DependentUpon>XrefPathExtractor.cs</DependentUpon>
62+
</Compile>
63+
</ItemGroup>
5964

6065
</Project>

Test/TestViewPort.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ public void TestInsideViewport()
9898
tr.Commit();
9999
}
100100

101+
102+
int totalCount = 0;
101103
using (Transaction tr = db.TransactionManager.StartTransaction())
102104
{
103105
BlockTableRecord blockTableRecord = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db),
@@ -121,11 +123,12 @@ public void TestInsideViewport()
121123
if (ent is BlockReference block)
122124
{
123125
ed.WriteMessage($"\nEntity Block in Viewport: {entId.Handle} ({block.Name})");
126+
totalCount += 1;
124127
}
125128

126129
}
127130
}
128-
131+
ed.WriteMessage($"Total block inside: {totalCount}");
129132
tr.Commit();
130133
}
131134
}

Test/XrefList.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using Autodesk.AutoCAD.ApplicationServices;
2+
using Autodesk.AutoCAD.DatabaseServices;
3+
using Autodesk.AutoCAD.EditorInput;
4+
using Autodesk.AutoCAD.Runtime;
5+
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
6+
7+
namespace Test
8+
{
9+
/// <summary>
10+
/// http://www.theswamp.org/index.php?topic=22884
11+
/// https://forums.autodesk.com/t5/net-forum/get-list-of-xrefs-in-net/td-p/4553291
12+
/// </summary>
13+
public static class ListXref
14+
{
15+
[CommandMethod("XRLST")]
16+
public static void GetXrefList()
17+
{
18+
Document doc = acadApp.DocumentManager.MdiActiveDocument;
19+
Database db = doc.Database;
20+
Editor ed = doc.Editor;
21+
XrefGraph xrg = db.GetHostDwgXrefGraph(true);
22+
GraphNodeCollection gnc = new GraphNodeCollection();
23+
for (int i = 0; i < xrg.NumNodes; i++)
24+
{
25+
gnc.Add(xrg.GetXrefNode(i));
26+
}
27+
for (int i = 1; i < xrg.NumNodes; i++)
28+
{
29+
XrefGraphNode xrgn = xrg.GetXrefNode(i);
30+
if (xrgn.IsNested)
31+
{
32+
for (int j = 0; j < xrgn.NumIn; j++)
33+
{
34+
XrefGraphNode parent = xrg.GetXrefNode(gnc.IndexOf(gnc[i].In(j)));
35+
ed.WriteMessage("\n{0}|{1} : {2}", parent.Name, xrgn.Name, getStatus(xrgn));
36+
}
37+
}
38+
else
39+
{
40+
ed.WriteMessage("\n{0} : {1}", xrgn.Name, getStatus(xrgn));
41+
}
42+
}
43+
}
44+
45+
private static string getStatus(XrefGraphNode xrgn)
46+
{
47+
string result = string.Empty;
48+
switch (xrgn.XrefStatus)
49+
{
50+
case XrefStatus.FileNotFound:
51+
result = "File not found";
52+
break;
53+
case XrefStatus.Resolved:
54+
result = "Resolved";
55+
break;
56+
case XrefStatus.Unloaded:
57+
result = "Unloaded";
58+
break;
59+
case XrefStatus.Unreferenced:
60+
result = "Unreferenced";
61+
break;
62+
case XrefStatus.Unresolved:
63+
result = "Unresolved";
64+
break;
65+
}
66+
return result;
67+
}
68+
}
69+
}

Test/XrefPathExtractor.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using Autodesk.AutoCAD.ApplicationServices;
2+
using Autodesk.AutoCAD.DatabaseServices;
3+
using Autodesk.AutoCAD.Runtime;
4+
using Autodesk.AutoCAD.EditorInput;
5+
using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
6+
7+
namespace Test;
8+
9+
/// <summary>
10+
/// https://adndevblog.typepad.com/autocad/2012/06/finding-all-xrefs-in-the-current-database-using-cnet.html
11+
/// http://www.theswamp.org/index.php?topic=22884
12+
/// https://forums.autodesk.com/t5/net-forum/get-list-of-xrefs-in-net/td-p/4553291
13+
/// </summary>
14+
public class XrefPathExtractor
15+
{
16+
[CommandMethod("XrefGraph")]
17+
public static void XrefGraph()
18+
19+
{
20+
Document doc = Application.DocumentManager.MdiActiveDocument;
21+
22+
Database db = doc.Database;
23+
24+
Editor ed = doc.Editor;
25+
26+
using (Transaction Tx = db.TransactionManager.StartTransaction())
27+
28+
{
29+
ed.WriteMessage("\n---Resolving the XRefs------------------");
30+
31+
db.ResolveXrefs(true, false);
32+
33+
XrefGraph xg = db.GetHostDwgXrefGraph(true);
34+
35+
ed.WriteMessage("\n---XRef's Graph-------------------------");
36+
37+
ed.WriteMessage("\nCURRENT DRAWING");
38+
39+
GraphNode? root = xg.RootNode;
40+
41+
PrintChildren(root, "|-------", ed, Tx);
42+
43+
ed.WriteMessage("\n----------------------------------------\n");
44+
}
45+
}
46+
47+
48+
// Recursively prints out information about the XRef's hierarchy
49+
50+
private static void PrintChildren(GraphNode? iRoot, string iIndent,
51+
Editor iEd, Transaction iTx)
52+
53+
{
54+
for (int o = 0; o < iRoot!.NumOut; o++)
55+
56+
{
57+
XrefGraphNode? child = iRoot.Out(o) as XrefGraphNode;
58+
59+
if (child.XrefStatus == XrefStatus.Resolved)
60+
61+
{
62+
BlockTableRecord? bl =
63+
iTx.GetObject(child.BlockTableRecordId, OpenMode.ForRead)
64+
as BlockTableRecord;
65+
66+
iEd.WriteMessage("\n" + iIndent + child.Database.Filename);
67+
68+
// Name of the Xref (found name)
69+
70+
// You can find the original path too:
71+
72+
//if (bl.IsFromExternalReference == true)
73+
74+
// i_ed.WriteMessage("\n" + i_indent + "Xref path name: "
75+
76+
// + bl.PathName);
77+
78+
PrintChildren(child, "| " + iIndent, iEd, iTx);
79+
}
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)