Skip to content

Commit 7c68010

Browse files
committed
add more test
1 parent 37aeae2 commit 7c68010

File tree

4 files changed

+305
-1
lines changed

4 files changed

+305
-1
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ output
5050
packages/
5151
*.html
5252
*.htm
53-
53+
build

Test/ExportBlockCoordinate.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ public void Export()
7171
{
7272
BlockTableRecord? blockTableRecord = tr.GetObject(blockRef.DynamicBlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
7373
AnonymousName = blockTableRecord?.Name?? String.Empty;
74+
DynamicBlockReferencePropertyCollection pc = blockRef.DynamicBlockReferencePropertyCollection;
75+
foreach (DynamicBlockReferenceProperty blockReferenceProperty in pc)
76+
{
77+
editor.WriteMessage($"name:{blockReferenceProperty.PropertyName}:value{blockReferenceProperty.Value}");
78+
}
79+
7480
string blockName = blockRef.Name;
7581
Point3d location = blockRef.Position;
7682
double rotation = ToDeg(blockRef.Rotation);

Test/ExportBlockReferenceAttrs.cs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
using Autodesk.AutoCAD.ApplicationServices;
2+
using Autodesk.AutoCAD.DatabaseServices;
3+
using Autodesk.AutoCAD.EditorInput;
4+
using Autodesk.AutoCAD.Runtime;
5+
using Application = Autodesk.AutoCAD.ApplicationServices.Application;
6+
7+
namespace Test;
8+
9+
/// <summary>
10+
/// Testing How to get block attribute from autocad.
11+
/// https://stackoverflow.com/questions/68068857/autocad-2021-net-api-getting-all-blockreferences-in-a-database
12+
///
13+
/// </summary>
14+
public class ExportBlockReferenceAttrs
15+
{
16+
[CommandMethod("Export Block Attrs")]
17+
public void Export()
18+
{
19+
Document doc = Application.DocumentManager.MdiActiveDocument;
20+
Editor editor = doc.Editor;
21+
Database db = doc.Database;
22+
PromptSelectionResult psr = editor.GetSelection();
23+
if (psr.Status != PromptStatus.OK)
24+
return;
25+
ObjectIdCollection idCol = new ObjectIdCollection(psr.Value.GetObjectIds());
26+
editor.WriteMessage($"Count Ids {idCol.Count}");
27+
using Transaction tr = db.TransactionManager.StartTransaction();
28+
foreach (ObjectId entityId in idCol)
29+
{
30+
Entity? entity = tr.GetObject(entityId, OpenMode.ForRead) as Entity;
31+
if (entity is BlockReference blockRef)
32+
{
33+
if (blockRef.IsDynamicBlock)
34+
{
35+
DynamicBlockReferencePropertyCollection pc = blockRef.DynamicBlockReferencePropertyCollection;
36+
foreach (DynamicBlockReferenceProperty blockReferenceProperty in pc)
37+
{
38+
editor.WriteMessage(
39+
$"name:{blockReferenceProperty.PropertyName}, value:{blockReferenceProperty.Value}\n");
40+
}
41+
42+
string blockName = blockRef.Name;
43+
editor.WriteMessage($"block Name:{blockName}, hander{blockRef.Handle.Value}");
44+
}
45+
else
46+
{
47+
if (blockRef.AttributeCollection.Count > 0)
48+
{
49+
foreach (ObjectId attId in blockRef.AttributeCollection)
50+
{
51+
AttributeReference? attRef = tr.GetObject(attId, OpenMode.ForRead) as AttributeReference;
52+
if (attRef != null)
53+
{
54+
editor.WriteMessage($"\n -> {attRef.Tag} = {attRef.TextString}\n");
55+
}
56+
}
57+
}
58+
}
59+
}
60+
}
61+
62+
tr.Abort();
63+
}
64+
65+
[CommandMethod("EXPORTBLOCKS")]
66+
public void ExportAllBlocks()
67+
{
68+
Document doc = Application.DocumentManager.MdiActiveDocument;
69+
Editor ed = doc.Editor;
70+
Database db = doc.Database;
71+
72+
using (Transaction tr = db.TransactionManager.StartTransaction())
73+
{
74+
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
75+
LayoutManager lm = LayoutManager.Current;
76+
string currentLayoutName = lm.CurrentLayout;
77+
78+
DBDictionary layoutDict = (DBDictionary)tr.GetObject(db.LayoutDictionaryId, OpenMode.ForRead);
79+
Layout? layout = null;
80+
foreach (DBDictionaryEntry entry in layoutDict)
81+
{
82+
Layout l = (Layout)tr.GetObject(entry.Value, OpenMode.ForRead);
83+
if (l.LayoutName == currentLayoutName)
84+
{
85+
layout = l;
86+
break;
87+
}
88+
}
89+
90+
if (layout == null)
91+
{
92+
ed.WriteMessage("Layout not found.");
93+
return;
94+
}
95+
96+
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(layout.BlockTableRecordId, OpenMode.ForRead);
97+
98+
int count = 0;
99+
foreach (ObjectId objId in btr)
100+
{
101+
Entity? ent = tr.GetObject(objId, OpenMode.ForRead) as Entity;
102+
if (ent is BlockReference blockRef)
103+
{
104+
count++;
105+
ed.WriteMessage($"\n[{count}] Block Name: {blockRef.Name}, Handle: {blockRef.Handle}");
106+
107+
if (blockRef.IsDynamicBlock)
108+
{
109+
DynamicBlockReferencePropertyCollection
110+
props = blockRef.DynamicBlockReferencePropertyCollection;
111+
foreach (DynamicBlockReferenceProperty prop in props)
112+
{
113+
ed.WriteMessage($"\n -> DynamicProp: {prop.PropertyName} = {prop.Value}");
114+
}
115+
}
116+
117+
if (blockRef.AttributeCollection.Count > 0)
118+
{
119+
foreach (ObjectId attId in blockRef.AttributeCollection)
120+
{
121+
AttributeReference? attRef = tr.GetObject(attId, OpenMode.ForRead) as AttributeReference;
122+
if (attRef != null)
123+
{
124+
ed.WriteMessage($"\n -> Attribute: {attRef.Tag} = {attRef.TextString}");
125+
}
126+
}
127+
}
128+
}
129+
}
130+
131+
ed.WriteMessage($"\nTotal BlockReference trong layout '{currentLayoutName}': {count}");
132+
tr.Commit();
133+
}
134+
}
135+
}

Test/TestViewPort.cs

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
using System.Collections.Generic;
2+
using System.Windows.Forms;
3+
using Autodesk.AutoCAD.ApplicationServices;
4+
using Autodesk.AutoCAD.DatabaseServices;
5+
using Autodesk.AutoCAD.EditorInput;
6+
using Autodesk.AutoCAD.Geometry;
7+
using Autodesk.AutoCAD.Runtime;
8+
9+
namespace Test;
10+
11+
/// <summary>
12+
/// https://forums.autodesk.com/t5/net-forum/viewport-get-entities-from-viewport/td-p/5832707
13+
/// </summary>
14+
public class TestViewPort
15+
{
16+
17+
[CommandMethod("TestInsideViewport")]
18+
public void TestInsideViewport()
19+
{
20+
Document doc = null;
21+
Database db = null;
22+
Editor ed = null;
23+
24+
try
25+
{
26+
doc = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
27+
db = doc.Database;
28+
ed = doc.Editor;
29+
LayoutManager layoutManager = LayoutManager.Current;
30+
31+
string layoutName = layoutManager.CurrentLayout;
32+
ed.WriteMessage($"\nLayout Name: {layoutName}");
33+
ObjectId layoutId = layoutManager.GetLayoutId(layoutName);
34+
35+
ObjectId vpId = ObjectId.Null;
36+
using (Transaction tr = db.TransactionManager.StartTransaction())
37+
{
38+
Layout layout = (Layout)tr.GetObject(layoutId, OpenMode.ForRead);
39+
var vpIds = layout.GetViewports();
40+
if (vpIds.Count > 1)
41+
vpId = vpIds[1]; // First Viewport is Paperspace itself
42+
tr.Commit();
43+
}
44+
45+
if (vpId.IsNull)
46+
throw new Exception(ErrorStatus.Vetoed, "viewport have issue");
47+
48+
List<Point3d>? vpOutlinePntsInMs = null;
49+
using (Transaction tr = db.TransactionManager.StartTransaction())
50+
{
51+
Viewport vp = (Viewport)tr.GetObject(vpId, OpenMode.ForRead);
52+
Polyline? vpOutlineInMs = null;
53+
54+
if (vp.NonRectClipOn)
55+
{
56+
ObjectId vpClipId = vp.NonRectClipEntityId;
57+
Entity vpBoundary = (Entity)tr.GetObject(vpClipId, OpenMode.ForRead);
58+
vpOutlineInMs = (Polyline)vpBoundary.Clone();
59+
}
60+
else
61+
{
62+
Extents3d vpExt = vp.GeometricExtents;
63+
vpOutlineInMs = new Polyline(4);
64+
vpOutlineInMs.AddVertexAt(0, new Point2d(vpExt.MinPoint.X, vpExt.MinPoint.Y), 0, 0, 0);
65+
vpOutlineInMs.AddVertexAt(1, new Point2d(vpExt.MaxPoint.X, vpExt.MinPoint.Y), 0, 0, 0);
66+
vpOutlineInMs.AddVertexAt(2, new Point2d(vpExt.MaxPoint.X, vpExt.MaxPoint.Y), 0, 0, 0);
67+
vpOutlineInMs.AddVertexAt(3, new Point2d(vpExt.MinPoint.X, vpExt.MaxPoint.Y), 0, 0, 0);
68+
vpOutlineInMs.Closed = true;
69+
}
70+
71+
Point3d center = new Point3d(vp.ViewCenter.X, vp.ViewCenter.Y, 0.0);
72+
Matrix3d msToPs =
73+
Matrix3d.Displacement(new Vector3d(vp.CenterPoint.X - center.X, vp.CenterPoint.Y - center.Y, 0.0)) *
74+
Matrix3d.Scaling(vp.CustomScale, center) *
75+
Matrix3d.Rotation(vp.TwistAngle, Vector3d.ZAxis, Point3d.Origin) *
76+
Matrix3d.WorldToPlane(new Plane(vp.ViewTarget, vp.ViewDirection));
77+
78+
vpOutlineInMs.TransformBy(msToPs.Inverse());
79+
80+
vpOutlinePntsInMs = new List<Point3d>();
81+
for (int i = 0; i < vpOutlineInMs.NumberOfVertices; i++)
82+
vpOutlinePntsInMs.Add(vpOutlineInMs.GetPoint3dAt(i));
83+
84+
// Draw polyline in model space
85+
Polyline transformedPoly = new Polyline();
86+
for (int i = 0; i < vpOutlinePntsInMs.Count; i++)
87+
transformedPoly.AddVertexAt(i, new Point2d(vpOutlinePntsInMs[i].X, vpOutlinePntsInMs[i].Y), 0, 0,
88+
0);
89+
transformedPoly.Closed = true;
90+
transformedPoly.ColorIndex = 1; // Red
91+
92+
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
93+
BlockTableRecord ms =
94+
(BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
95+
ms.AppendEntity(transformedPoly);
96+
tr.AddNewlyCreatedDBObject(transformedPoly, true);
97+
98+
tr.Commit();
99+
}
100+
101+
using (Transaction tr = db.TransactionManager.StartTransaction())
102+
{
103+
BlockTableRecord blockTableRecord = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db),
104+
OpenMode.ForRead);
105+
foreach (ObjectId entId in blockTableRecord)
106+
{
107+
Entity ent = (Entity)tr.GetObject(entId, OpenMode.ForRead);
108+
if (ent is Polyline)
109+
continue;
110+
111+
var pt1 = ent.GeometricExtents.MinPoint;
112+
var pt2 = new Point3d(ent.GeometricExtents.MaxPoint.X, ent.GeometricExtents.MinPoint.Y, 0);
113+
var pt3 = ent.GeometricExtents.MaxPoint;
114+
var pt4 = new Point3d(ent.GeometricExtents.MinPoint.X, ent.GeometricExtents.MaxPoint.Y, 0);
115+
116+
if (IsInside2D(vpOutlinePntsInMs.ToArray(), pt1) &&
117+
IsInside2D(vpOutlinePntsInMs.ToArray(), pt2) &&
118+
IsInside2D(vpOutlinePntsInMs.ToArray(), pt3) &&
119+
IsInside2D(vpOutlinePntsInMs.ToArray(), pt4))
120+
{
121+
if (ent is BlockReference block)
122+
{
123+
ed.WriteMessage($"\nEntity Block in Viewport: {entId.Handle} ({block.Name})");
124+
}
125+
126+
}
127+
}
128+
129+
tr.Commit();
130+
}
131+
}
132+
catch (Exception ex)
133+
{
134+
if (ed != null)
135+
ed.WriteMessage("\n Error: " + ex.ToString());
136+
else
137+
MessageBox.Show("Error: " + ex.Message, "TestInsideViewport", MessageBoxButtons.OK,
138+
MessageBoxIcon.Error);
139+
}
140+
}
141+
private static bool IsInside2D(Point3d[] polyPoints, Point3d entPoint)
142+
{
143+
bool oddNodes = false;
144+
145+
146+
int i, j = polyPoints.Length - 1;
147+
148+
for (i = 0; i < polyPoints.Length; i++)
149+
{
150+
Point3d pp_i = polyPoints[i];
151+
Point3d pp_j = polyPoints[j];
152+
153+
if ((pp_i.Y < entPoint.Y && pp_j.Y >= entPoint.Y || pp_j.Y < entPoint.Y && pp_i.Y >= entPoint.Y) &&
154+
(pp_i.X <= entPoint.X || pp_j.X <= entPoint.X))
155+
oddNodes ^= (pp_i.X + (entPoint.Y - pp_i.Y) / (pp_j.Y - pp_i.Y) * (pp_j.X - pp_i.X) < entPoint.X);
156+
157+
j = i;
158+
}
159+
160+
return oddNodes;
161+
}
162+
163+
}

0 commit comments

Comments
 (0)