-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters