Skip to content

Commit

Permalink
add export coordinate
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed May 24, 2023
1 parent 04c8324 commit 24900d7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
11 changes: 11 additions & 0 deletions AddInManager.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "TestVB", "TestVB\TestVB.vbp
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug A21|Any CPU = Debug A21|Any CPU
Debug A22|Any CPU = Debug A22|Any CPU
Debug A23|Any CPU = Debug A23|Any CPU
Installer|Any CPU = Installer|Any CPU
Expand All @@ -32,30 +33,40 @@ Global
{E3C87D34-638C-47A0-A73A-D967B119458D}.Installer|Any CPU.Build.0 = Release|Any CPU
{E3C87D34-638C-47A0-A73A-D967B119458D}.Release A23|Any CPU.ActiveCfg = Release|Any CPU
{E3C87D34-638C-47A0-A73A-D967B119458D}.Debug A22|Any CPU.ActiveCfg = Debug|Any CPU
{E3C87D34-638C-47A0-A73A-D967B119458D}.Debug A21|Any CPU.ActiveCfg = Debug|Any CPU
{E3C87D34-638C-47A0-A73A-D967B119458D}.Debug A21|Any CPU.Build.0 = Debug|Any CPU
{5016ED6D-5A9A-4F59-AE49-CAA9615798F7}.Debug A23|Any CPU.ActiveCfg = Debug|Any CPU
{5016ED6D-5A9A-4F59-AE49-CAA9615798F7}.Installer|Any CPU.ActiveCfg = Release|Any CPU
{5016ED6D-5A9A-4F59-AE49-CAA9615798F7}.Installer|Any CPU.Build.0 = Release|Any CPU
{5016ED6D-5A9A-4F59-AE49-CAA9615798F7}.Release A23|Any CPU.ActiveCfg = Release|Any CPU
{5016ED6D-5A9A-4F59-AE49-CAA9615798F7}.Debug A22|Any CPU.ActiveCfg = Debug|Any CPU
{5016ED6D-5A9A-4F59-AE49-CAA9615798F7}.Debug A21|Any CPU.ActiveCfg = Debug|Any CPU
{5016ED6D-5A9A-4F59-AE49-CAA9615798F7}.Debug A21|Any CPU.Build.0 = Debug|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Installer|Any CPU.ActiveCfg = Release A23|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Release A23|Any CPU.ActiveCfg = Release A23|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Release A23|Any CPU.Build.0 = Release A23|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug A23|Any CPU.ActiveCfg = Debug A23|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug A23|Any CPU.Build.0 = Debug A23|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug A22|Any CPU.ActiveCfg = Debug A22|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug A22|Any CPU.Build.0 = Debug A22|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug A21|Any CPU.ActiveCfg = Debug A21|Any CPU
{1661572C-EF3A-4DD6-83BD-CB4239CE8CDD}.Debug A21|Any CPU.Build.0 = Debug A21|Any CPU
{E05305A7-28E1-4658-B505-E035FEDDD266}.Debug A23|Any CPU.ActiveCfg = Debug A23|Any CPU
{E05305A7-28E1-4658-B505-E035FEDDD266}.Debug A23|Any CPU.Build.0 = Debug A23|Any CPU
{E05305A7-28E1-4658-B505-E035FEDDD266}.Installer|Any CPU.ActiveCfg = Release A23|Any CPU
{E05305A7-28E1-4658-B505-E035FEDDD266}.Release A23|Any CPU.ActiveCfg = Release A23|Any CPU
{E05305A7-28E1-4658-B505-E035FEDDD266}.Release A23|Any CPU.Build.0 = Release A23|Any CPU
{E05305A7-28E1-4658-B505-E035FEDDD266}.Debug A22|Any CPU.ActiveCfg = Debug A22|Any CPU
{E05305A7-28E1-4658-B505-E035FEDDD266}.Debug A22|Any CPU.Build.0 = Debug A22|Any CPU
{E05305A7-28E1-4658-B505-E035FEDDD266}.Debug A21|Any CPU.ActiveCfg = Debug A22|Any CPU
{E05305A7-28E1-4658-B505-E035FEDDD266}.Debug A21|Any CPU.Build.0 = Debug A22|Any CPU
{C736FCE7-CAE8-474B-8A55-672BC5C1F39B}.Debug A23|Any CPU.ActiveCfg = Release A23|Any CPU
{C736FCE7-CAE8-474B-8A55-672BC5C1F39B}.Debug A23|Any CPU.Build.0 = Release A23|Any CPU
{C736FCE7-CAE8-474B-8A55-672BC5C1F39B}.Installer|Any CPU.ActiveCfg = Release A23|Any CPU
{C736FCE7-CAE8-474B-8A55-672BC5C1F39B}.Release A23|Any CPU.ActiveCfg = Release A23|Any CPU
{C736FCE7-CAE8-474B-8A55-672BC5C1F39B}.Release A23|Any CPU.Build.0 = Release A23|Any CPU
{C736FCE7-CAE8-474B-8A55-672BC5C1F39B}.Debug A21|Any CPU.ActiveCfg = Debug A23|Any CPU
{C736FCE7-CAE8-474B-8A55-672BC5C1F39B}.Debug A21|Any CPU.Build.0 = Debug A23|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
81 changes: 81 additions & 0 deletions Test/ExportBlockCoordinate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;

namespace Test;

public class ExportBlockCoordinate
{
[CommandMethod("ExportBlockCoordinate")]
public void Export()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor editor = doc.Editor;
Database db = doc.Database;

// Prompt the user for the layer name
PromptResult promptResult = editor.GetString("\nEnter the layer name: ");
if (promptResult.Status != PromptStatus.OK)
{
editor.WriteMessage("\nInvalid input. Layer name not provided.");
return;
}
string layerName = promptResult.StringResult;

// Display the Save File dialog
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "CSV Files (*.csv)|*.csv";
saveFileDialog.Title = "Save Block Information";
DialogResult dialogResult = saveFileDialog.ShowDialog();

if (dialogResult != DialogResult.OK)
{
editor.WriteMessage("\nInvalid input. Output file path not provided.");
return;
}
string filePath = saveFileDialog.FileName;

using (Transaction tr = db.TransactionManager.StartTransaction())
{
// Open the block table for read
BlockTable blockTable = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

// Open the current space block table record for read
BlockTableRecord spaceRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;

using (StreamWriter writer = new StreamWriter(filePath))
{
// Write the CSV header
writer.WriteLine("Block Name,X,Y,Z,Rotation");

// Iterate through all the entities in the current space
foreach (ObjectId entityId in spaceRecord)
{
Entity entity = tr.GetObject(entityId, OpenMode.ForRead) as Entity;

// Check if the entity is a block reference and on the specified layer
if (entity is BlockReference blockRef && blockRef.Layer.Equals(layerName, StringComparison.OrdinalIgnoreCase))
{
string blockName = blockRef.Name;
Point3d location = blockRef.Position;
double rotation = blockRef.Rotation;
// Write the block information to the CSV file
writer.WriteLine($"{blockName},{location.X},{location.Y},{location.Z},{rotation}");
}
}
}

tr.Commit();
}
editor.WriteMessage($"\nBlock information exported to: {filePath}");
Process.Start(filePath);

}
}
2 changes: 1 addition & 1 deletion Test/Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<PlatformTarget>x64</PlatformTarget>
<UseWindowsForms>true</UseWindowsForms>
<UseWpf>true</UseWpf>
<Configurations>Debug A22;Debug A23</Configurations>
<Configurations>Debug A21;Debug A22;Debug A23</Configurations>
<Configurations>$(Configurations);Release A20;Release A21;Release A22;Release A23</Configurations>
</PropertyGroup>
<PropertyGroup Condition="$(Configuration.Contains('A20'))">
Expand Down

0 comments on commit 24900d7

Please sign in to comment.