Когда я пытаюсь вставить блок с атрибутами в боковой загруженный DWG со следующим кодом, я получаю ошибку неправильной базы данных в линии brblock.attributecollection.appendattribute (ar); Полем Может ли кто -нибудь указать мне в правильном направлении того, что я делаю не так? Я попытался получить получение BlockTableRecord от BlockReference, но без повезло. < /P>
Заранее спасибо.using (Database targetDb = new Database(false, true))
{
using (Transaction tr = targetDb.TransactionManager.StartTransaction())
{
targetDb.ReadDwgFile(file, FileOpenMode.OpenForReadAndAllShare, true, "");
targetDb.CloseInput(true);
bool bFixErrors = true;
bool becho = true;
ImportAlstomFiles.TransformDwg.InsertTbmBlock(targetDb);
targetDb.Audit(bFixErrors, becho);
targetDb.SaveAs(file, DwgVersion.Current);
tr.Commit();
}
}
public class TransformDwg
{
public static void InsertTbm(Database db)
{
string[] files = Directory.GetFiles(@"C:\ProgramData\Autodesk\ApplicationPlugins\SpNwDo.bundle\Contents\Block\Alstom\", "*.dwg");
foreach (string file in files)
{
string blockname = Path.GetFileNameWithoutExtension(file);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
Database tmpDb = new Database(false, true);
tmpDb.ReadDwgFile(file, FileShare.Read, true, "");
db.Insert(blockname, tmpDb, true);
tr.Commit();
}
}
Point3d insPoint = new Point3d();
using (Transaction tr = db.TransactionManager.StartTransaction())
{
// Open BlockTable and BlockTableRecord
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
// Retrieve the inserted block definition
BlockTableRecord btrBlock = tr.GetObject(bt["Alstom_TBM_DATA01_S_N"], OpenMode.ForRead) as BlockTableRecord;
// Loop through the inserted block references and retrieve attribute values
foreach (ObjectId btrId in btr)
{
if (btrId.ObjectClass.DxfName == "INSERT")
{
BlockReference br = tr.GetObject(btrId, OpenMode.ForWrite) as BlockReference;
SymbolTableRecord str = tr.GetObject(br.DynamicBlockTableRecord, OpenMode.ForRead) as SymbolTableRecord;
using (BlockReference brBlock = new BlockReference(insPoint, btrBlock.ObjectId))
{
btr.UpgradeOpen();
// Append the new BlockReference to PaperSpace
btr.AppendEntity(brBlock);
tr.AddNewlyCreatedDBObject(brBlock, true);
// Loop through BlockTableRecord to create AttributeReferences
foreach (ObjectId btrBlockId in btrBlock)
{
AttributeDefinition ad = tr.GetObject(btrBlockId, OpenMode.ForWrite) as AttributeDefinition;
if (ad != null)
{
// Create a new AttributeReference in the correct database (db)
using (AttributeReference ar = new AttributeReference())
{
// Set the attribute values and transformation
ar.SetAttributeFromBlock(ad, brBlock.BlockTransform);
// Update the text for each attribute based on collected values
if (ar.Tag == "AT_DATA_ZONE_N")
ar.TextString = zone;
if (ar.Tag == "AT_DATA_STATION_N")
ar.TextString = station;
if (ar.Tag == "AT_DATA_BLOCKNUM_N")
ar.TextString = blocknum;
if (ar.Tag == "AT_DATA_DESC3_S_N")
ar.TextString = lijn1;
if (ar.Tag == "AT_DATA_DESC4_S_N")
ar.TextString = lijn2;
if (ar.Tag == "AT_DATA_DESC5_S_N")
ar.TextString = lijn3;
// Add the AttributeReference to the BlockReference's AttributeCollection
brBlock.AttributeCollection.AppendAttribute(ar);
// Ensure the attribute is added to the transaction
tr.AddNewlyCreatedDBObject(ar, true);
}
}
}
}
}
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/795 ... loaded-dwg
Вставьте блок с атрибутами, нагруженными DWG ⇐ C#
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Pyautocad - Автоматизация чертежа - импорт DWG - текст записи - импорт изображение
Anonymous » » в форуме Python - 0 Ответы
- 23 Просмотры
-
Последнее сообщение Anonymous
-