stephenlecomptejr
asked on
Need help with converting C# AutoCAD code to Visual Basic.
All, Please note the following code found at this link:
http://through-the-interface.typepad.com/through_the_interface/2007/07/updating-a-spec.html
I'm trying to convert this C# code to Visual Basic....
Here is what I've got so far but I run into issues with the next couple of For Each statements:
Please fix the syntax so that I may be able to use and test?
http://through-the-interface.typepad.com/through_the_interface/2007/07/updating-a-spec.html
I'm trying to convert this C# code to Visual Basic....
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
namespace BlockIterator
{
public class Commands
{
[CommandMethod("UA")]
public void UpdateAttribute()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// Have the user choose the block and attribute
// names, and the new attribute value
PromptResult pr =
ed.GetString(
"\nEnter name of block to search for: "
);
if (pr.Status != PromptStatus.OK)
return;
string blockName = pr.StringResult.ToUpper();
pr =
ed.GetString(
"\nEnter tag of attribute to update: "
);
if (pr.Status != PromptStatus.OK)
return;
string attbName = pr.StringResult.ToUpper();
pr =
ed.GetString(
"\nEnter new value for attribute: "
);
if (pr.Status != PromptStatus.OK)
return;
string attbValue = pr.StringResult;
UpdateAttributesInDatabase(
db,
blockName,
attbName,
attbValue
);
}
private void UpdateAttributesInDatabase(
Database db,
string blockName,
string attbName,
string attbValue
)
{
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
);
ed.Regen();
// Display the results
ed.WriteMessage(
"\nProcessing file: " + db.Filename
);
ed.WriteMessage(
"\nUpdated {0} instance{1} of " +
"attribute {2} in the modelspace.",
msCount,
msCount == 1 ? "" : "s",
attbName
);
ed.WriteMessage(
"\nUpdated {0} instance{1} of " +
"attribute {2} in the default paperspace.",
psCount,
psCount == 1 ? "" : "s",
attbName
);
}
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;
}
}
}
Here is what I've got so far but I run into issues with the next couple of For Each statements:
Public Function UpdateAttributesInBlock(ByVal btrID As ObjectId, ByVal blockName As String, ByVal attbName As String, ByVal attbValue As String)
Dim changedCount As Integer = 0
Dim doc As Document
doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Dim ed As Editor = doc.Editor
Dim btr As BlockTableRecord
Dim id As ObjectId = GetBlockTableRecordId(doc.Database, blockName)
Dim tr As Transaction = doc.TransactionManager.StartTransaction
Dim ent As Entity
Using tr
btr = TryCast(id.GetObject(OpenMode.ForRead), BlockTableRecord)
For Each id In btr
ent = tr.GetObject(entId, OpenMode.ForRead)
Next
End Using
End Function
Please fix the syntax so that I may be able to use and test?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for your reply!