Block mit den Attributen mit dem Seiten geladenen DWG einfügenC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Block mit den Attributen mit dem Seiten geladenen DWG einfügen

Post by Anonymous »

Wenn ich versuche, einen Block mit Attributen in einem von der Seite geladenen DWG mit dem folgenden Code einzufügen, erhalte ich die falsche Datenbank in der Zeile. . Kann mich jemand in die richtige Richtung dessen zeigen, was ich falsch mache? Ich habe versucht, den BlockTableRecord aus der Blockreferenz zu erhalten, aber ohne Glück. < /P>
Danke im Voraus. < /P>

Code: Select all

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);
}
}
}
}
}
}
}
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post