.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Delete a block by name then purge all .net C#

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
Anonymous
4117 Views, 3 Replies

Delete a block by name then purge all .net C#

So I have a situation where I have a known block, that I need to delete.

 

The problem is, once I delete it...it seems to take about three purges to clear all of the purged data.

 

I can type in Purge and then have to purge all three times.

 

What is the simplest way to simply delete a known block, and purge all of the data with one shot.

 

I then plan on reinserting the known block using a string to execute.

 

I am using code to update 40+ sheets in a directory.  The current code is using Database db = new Database(false,false).

 

Will simply saying db.Purge work in this case?  For the purge portion of this?

 

 

3 REPLIES 3
Message 2 of 4
Anonymous
in reply to: Anonymous

foreach (string fileName in fileNames)

 

{

if (fileName.EndsWith(

".dwg",

 

StringComparison.CurrentCultureIgnoreCase

)

)

{

Database db = new Database(false, false);

using (db)

 

{

try

 

{

ed.WriteMessage(

"\n\nProcessing file: " + fileName

 

);

db.ReadDwgFile(fileName, FileShare.ReadWrite, false, "");

int attributesChanged = UpdateAttributesInDatabase(db, blockName, attbName, attbValue, attOriginator, attOriginatorValue);

// Display the results

ed.WriteMessage("\nUpdated {0} instance{1} of " + "attribute {2}.", attributesChanged, attributesChanged == 1 ? "" : "s", attbName);

// Only save if we changed something

if (attributesChanged > 0)

 

{

db.SaveAs(

fileName,

DwgVersion.Current

);

saved++;

}

processed++;

}

catch (System.Exception ex)

 

{

ed.WriteMessage("\nProblem processing file: {0} - \"{1}\"", fileName, ex.Message);

 

problem++;

}

}

}

}

ed.WriteMessage("\n\nSuccessfully processed {0} files, of which {1} had " + "attributes to update and an additional {2} had errors " + "during reading/processing.", processed, saved, problem);

 

}

private int UpdateAttributesInDatabase(Database db, string blockName, string attbName, string attbValue, string attOriginator, string attOriginatorValue)

 

{

Document doc =

Application.DocumentManager.MdiActiveDocument;

Editor ed = doc.Editor;

// Get the IDs of the spaces we want to process

// and simply call a function to process each

 

ObjectId msId, psId;

Transaction tr =

db.TransactionManager.StartTransaction();

using (tr)

 

{

BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

msId = bt[BlockTableRecord.ModelSpace];

psId = bt[BlockTableRecord.PaperSpace];

// Not needed, but quicker than aborting

 

tr.Commit();

}

int msCount = UpdateAttributesInBlock(msId, blockName, attbName, attbValue);

int psCount = UpdateAttributesInBlock(psId, blockName, attbName, attbValue);

return msCount + psCount;

 

}

 

private int UpdateAttributesInBlock(ObjectId btrId, string blockName, string attbName, string attbValue)

 

{

// Will return the number of attributes modified

int changedCount = 0;

 

Document doc =

Application.DocumentManager.MdiActiveDocument;

Database db = doc.Database;

Editor ed = doc.Editor;

Transaction tr =

doc.TransactionManager.StartTransaction();

using (tr)

 

{

BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);

 

// Test each entity in the container...

 

 

foreach (ObjectId entId in btr)

 

{

Entity ent = tr.GetObject(entId, OpenMode.ForRead)

as Entity;

if (ent != null)

 

{

BlockReference br = ent as BlockReference;

if (br != null)

 

{

BlockTableRecord bd = (BlockTableRecord)tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);

// ... to see whether it's a block with

// the name we're after

if (bd.Name.ToUpper() == blockName)

 

{

// Check each of the attributes...

foreach (

ObjectId arId in br.AttributeCollection

 

)

{

DBObject obj = tr.GetObject(arId, OpenMode.ForRead);

AttributeReference ar = obj as AttributeReference;

if (ar != null)

 

{

// ... to see whether it has

// the tag we're after

if (ar.Tag.ToUpper() == attbName)

 

{

// If so, update the value

// and increment the counter

 

ar.UpgradeOpen();

ar.TextString = attbValue;

ar.DowngradeOpen();

changedCount++;

}

}

}

}

// Recurse for nested blocks

 

changedCount += UpdateAttributesInBlock(br.BlockTableRecord, blockName, attbName, attbValue);

}

}

}

tr.Commit();

}

return changedCount;

 

}

}

 

Message 3 of 4
Anonymous
in reply to: Anonymous

Ok I figured out how to purge all via command line.  I will just need to run it three times.

 

-PU

all

*

n

 

Courtesy of this post:

https://forums.autodesk.com/t5/autocad-forum/deleting-block-names-in-the-block-file-list-in-2016/td-...

Message 4 of 4
Anonymous
in reply to: Anonymous

I have now found out how to delete a block via .net programming.

 

Courtesy of this post:

https://forums.autodesk.com/t5/net/delete-block-from-autocad-drawing/td-p/5533072

A simple copy and paste and BOOM, done. 🙂

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators


Autodesk Design & Make Report