[CommandMethod("myTestCommand")]
public void myTestCommand()
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptSaveFileOptions opts = new PromptSaveFileOptions("Select location drawing file");
opts.Filter = "Drawing (*.dwg)|*.dwg";
PromptFileNameResult pr = ed.GetFileNameForSave(opts);
string filePath = pr.StringResult;
Database xdb = null;
try
{
xdb = new Database(false, false);
xdb.ReadDwgFile(filePath, FileOpenMode.OpenForReadAndAllShare, true, "");
xdb.CloseInput(true);
using (Transaction tr = xdb.TransactionManager.StartTransaction())
{
ObjectIdCollection ids = new ObjectIdCollection();
using (BlockTable table = tr.GetObject(xdb.BlockTableId, OpenMode.ForWrite) as BlockTable)
using (BlockTableRecord btr = (BlockTableRecord)tr.GetObject(table[BlockTableRecord.ModelSpace], OpenMode.ForRead, false))
foreach (ObjectId id in btr)
{
if (id.ObjectClass.Name != Constants.AecbDbDuctFitting)
continue;
using (DuctFitting ductFitting = (DuctFitting)tr.GetObject(id, OpenMode.ForRead))
{
string partGuid = PartManager.GetPartGuid(ductFitting);
DataExpandedTable exTable = getExTableFromCatalog(ductFitting, partGuid);
if (exTable == null)
continue;
DataRecordCollection recordCol = exTable.DataRecords;
ductFitting.UpgradeOpen();
//метод которые вызывает подмену и появления дисконектов
Autodesk.Aec.Building.ApplicationServices.PartManager.ModifyPartViaRecord(exTable, recordCol[0], ductFitting);
if (exTable != null)
exTable.Dispose();
}
}
tr.Commit();
}
xdb.SaveAs(filePath, DwgVersion.Current);
}
catch (System.Exception e)
{
ed.WriteMessage("Fittings draw error: " + e.Message);
}
finally
{
if (xdb != null)
xdb.Dispose();
}
}
private DataExpandedTable getExTableFromCatalog(DuctFitting ductFitting, string partGuid)
{
DataQuery query = new DataQuery();
query.Domain = Domain.DuctComponent;
query.PartGuid = partGuid;
//добавляем параметры, размеры которые нам необходимы.
//для примера возьмем размеры самого фиттинга
//с заменой одного параметра "MyPropety"
using(DataRecord dataRecord = Autodesk.Aec.Building.ApplicationServices.PartManager.GetPartData(ductFitting))
foreach (DataField df in dataRecord.DataFields)
{
//заполняем только основными значениями. чтобы геометрия оставалась на прежнем месте.
if (df.RawDataSource != DataSource.ConstantLists)
continue;
if (df.Name == "MyProperty")
{
query.AddSizeParameter(df.Context, df.Index, "myValue");
continue;
}
if (df.Type == CatalogDataType.Bool)
query.AddSizeParameter(df.Context, df.Index, df.ValueBoolean);
if (df.Type == CatalogDataType.Double)
query.AddSizeParameter(df.Context, df.Index, df.ValueDouble);
if (df.Type == CatalogDataType.Int)
query.AddSizeParameter(df.Context, df.Index, df.ValueInteger);
if (df.Type == CatalogDataType.String)
query.AddSizeParameter(df.Context, df.Index, df.ValueString);
}
DataExpandedTable exTable = null;
try
{
exTable = Autodesk.Aec.Building.ApplicationServices.PartManager.GetPartTableIncludeCustomSize(query, 1);
}
catch
{
}
if (query != null)
query.Dispose();
return exTable;
}