The "EATTEXT" command in AutoCAD can extract data such as attribute text and display it inside a table. Here is a sample code to do it programmatically using the DataExtraction API and to create a table that links with the data.
1) Copy the attached "MyBlock.dwg" to "C:\Temp" folder.
3) Run the command and select a point when prompted for the insertion point of the table.
5) Right-click on the table and select "Update Table Data Links" option to refresh the values.
// Add the AcDx.dll reference from the inc folder
using Autodesk.AutoCAD.DataExtraction;
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
// Copy the attached "MyBlock.dwg" to C:\Temp for testing
// The Dxe file will be created at runtime in C:\Temp if not found
const string dxePath = @"C:\Temp\MyData.dxe";
const string dwgFolder = @"C:\Temp\";
const string dwgName = "MyBlock.dwg";
if (System.IO.File.Exists(dxePath) == false)
{
// Create the DXE file with the information that we want to extract
DxExtractionSettings setting = new DxExtractionSettings();
IDxFileReference dxFileReference
= new DxFileReference(dwgFolder, dwgFolder + dwgName);
setting.DrawingDataExtractor.Settings.DrawingList.AddFile
(dxFileReference);
setting.DrawingDataExtractor.DiscoverTypesAndProperties
(dwgFolder + dwgName);
List<IDxTypeDescriptor> types
= setting.DrawingDataExtractor.DiscoveredTypesAndProperties;
List<string> selectedTypes = new List<string>();
List<string> selectedProps = new List<string>();
foreach (IDxTypeDescriptor td in types)
{
if (td.GlobalName.Equals("BlockReferenceTypeDescriptor.Test"))
selectedTypes.Add(td.GlobalName);
foreach (IDxPropertyDescriptor pd in td.Properties)
{
if (pd.GlobalName.Equals("AcDxObjectTypeGlobalName") ||
pd.GlobalName.Equals("AcDxObjectTypeName") ||
pd.GlobalName.Equals("BlockReferenceAttribute.NAME") ||
pd.GlobalName.Equals("BlockReferenceAttribute.PLACE"))
{
if (!selectedProps.Contains(pd.GlobalName))
selectedProps.Add(pd.GlobalName);
}
}
}
setting.DrawingDataExtractor.Settings.ExtractFlags
= ExtractFlags.Nested | ExtractFlags.Xref;
setting.DrawingDataExtractor.Settings.SetSelectedTypesAndProperties
(types, selectedTypes, selectedProps);
setting.OutputSettings.DataCellStyle = "Data";
setting.OutputSettings.FileOutputType = AdoOutput.OutputType.xml;
setting.OutputSettings.HeaderCellStyle = "Header";
setting.OutputSettings.ManuallySetupTable = true;
setting.OutputSettings.OuputFlags = DxOuputFlags.Table;
setting.OutputSettings.TableStyleId = db.Tablestyle;
setting.OutputSettings.TableStyleName = "Standard";
setting.OutputSettings.TitleCellStyle = "Title";
setting.OutputSettings.UsePropertyNameAsColumnHeader = false;
setting.Save(dxePath);
}
//Create a DataLink
ObjectId dlId = ObjectId.Null;
DataLinkManager dlm = db.DataLinkManager;
using (DataLink dl = new DataLink())
{
String dataLinkName = "MyDataLink2";
dlId = dlm.GetDataLink(dataLinkName);
if(dlId.IsNull)
{
// create a datalink
dl.ConnectionString = dxePath;
dl.ToolTip = "My Data Link";
dl.Name = dataLinkName;
DataAdapter da
= DataAdapterManager.GetDataAdapter
("Autodesk.AutoCAD.DataExtraction.DxDataLinkAdapter");
if (da != null)
dl.DataAdapterId = da.DataAdapterId;
dlId = dlm.AddDataLink(dl);
}
}
// Ask for the table insertion point
PromptPointResult pr = ed.GetPoint("\nEnter table insertion point: ");
if (pr.Status != PromptStatus.OK)
return;
// Create a table
ObjectId tableId = ObjectId.Null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Table table = new Table();
// 2 rows and 4 columns
table.SetSize(2, 4);
table.Position = pr.Value;
//Add the Table to the drawing database
BlockTable bt = tr.GetObject(
db.BlockTableId,
OpenMode.ForWrite
) as BlockTable;
BlockTableRecord btr = tr.GetObject
(
bt[BlockTableRecord.ModelSpace],
OpenMode.ForWrite
) as BlockTableRecord;
tableId = btr.AppendEntity(table);
tr.AddNewlyCreatedDBObject(table, true);
//table.Cells.SetDataLink(dlId, false);
table.SetDataLink(1, 0, dlId, true);
//Generate the layout
table.GenerateLayout();
tr.Commit();
}
Recent Comments