Message 1 of 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I want to update Attribute update by using input from form Text box.
But attribute is not updating to the value entered. But if we give any default value to text box then it is updating to that value. other wise it is coming as blank.
Currently the program extracting value from the code to the other sub.
Could any one help me in this?
namespace SDRevisionUpdate
{
public partial class Revision : Form
{
public Revision()
{
InitializeComponent();
}
public void Revision_Load(object sender, EventArgs e)
{
}
private void RunBttn_Click(object sender, EventArgs e)
{
var dwg = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
var ed = dwg.Editor;
var fileNames = GetFileNames();
if (fileNames is null)
{
ed.WriteMessage("\n*Cancel*");
return;
}
ProcessDwgs(fileNames);
}
private IEnumerable<string> GetFileNames()
{
IEnumerable<string> files = null;
using (var dialog = new OpenFileDialog()
{
DefaultExt = ".dwg",
Multiselect = true,
Filter = "dwg files|*.dwg"
})
{
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
files = dialog.FileNames;
}
}
return files;
}
private void ProcessDwgs(IEnumerable<string> dwgFiles)
{
// Use Acad's progress bar, so that AutoCAD would not looks like
// frozen during long processing
using (var prog = new ProgressMeter())
{
prog.SetLimit(dwgFiles.Count());
foreach (var dwgFile in dwgFiles)
{
prog.Start("Processing DWG files...");
prog.MeterProgress();
System.Windows.Forms.Application.DoEvents();
using (var db = new Database(false, true))
{
db.ReadDwgFile(dwgFile, FileOpenMode.OpenForReadAndWriteNoShare, true, null);
DoSomethingInDwg(db);
// Save changed database
string prefix = "Acad-";
string newFileName = Path.Combine(Path.GetDirectoryName(db.Filename), prefix + Path.GetFileName(db.Filename));
db.SaveAs(newFileName, true, DwgVersion.Current, default);
System.Windows.MessageBox.Show("Process Complted Successfully", "Revision Updaate", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
prog.Stop();
}
}
public static bool DoSomethingInDwg(Database db)
{
try
{
var Revision = new Revision();
//Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable blkTable = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord blkRecord = (BlockTableRecord)tr.GetObject(blkTable["Tblock_ATT"], OpenMode.ForRead);
if (blkRecord.HasAttributeDefinitions)
{
ObjectIdCollection refIDs = blkRecord.GetBlockReferenceIds(true, false);
BlockReference bRef = (BlockReference)tr.GetObject(refIDs[0], OpenMode.ForRead);
AttributeReference attA = null;
AttributeReference attB = null;
AttributeReference attA1 = null;
AttributeReference attA2 = null;
AttributeReference attA3 = null;
AttributeReference attA4 = null;
AttributeReference attB1 = null;
AttributeReference attB2 = null;
AttributeReference attB3 = null;
AttributeReference attB4 = null;
foreach (ObjectId id in bRef.AttributeCollection)
{
var att = (AttributeReference)tr.GetObject(id, OpenMode.ForRead);
if (att.Tag.ToUpper() == "REV") attA = att;
if (att.Tag.ToUpper() == "DATE") attB = att;
if (att.Tag.ToUpper() == "REV1") attA1 = att;
if (att.Tag.ToUpper() == "DESC1") attA2 = att;
if (att.Tag.ToUpper() == "REV1_DATE") attA3 = att;
if (att.Tag.ToUpper() == "REV1_BY") attA4 = att;
if (att.Tag.ToUpper() == "REV2") attB1 = att;
if (att.Tag.ToUpper() == "DESC2") attB2 = att;
if (att.Tag.ToUpper() == "REV2_DATE") attB3 = att;
if (att.Tag.ToUpper() == "REV2_BY") attB4 = att;
}
if (attA.TextString == "A")
{
attB1.UpgradeOpen();
attB1.TextString = "B";
attB1.DowngradeOpen();
attB2.UpgradeOpen();
attB2.TextString = Revision.RevDesc.Text;
attB2.DowngradeOpen();
attB3.UpgradeOpen();
attB3.TextString = Revision.RevDate.Text;
attB3.DowngradeOpen();
attB4.UpgradeOpen();
attB4.TextString = Revision.ChkBy.Text;
attB4.DowngradeOpen();
}
switch (attA.TextString)
{
case ("A"):
attA.UpgradeOpen();
attA.TextString = "B";
attA.DowngradeOpen();
attB.UpgradeOpen();
attB.TextString = Revision.SubDate.Text;
attB.DowngradeOpen();
break;
}
}
tr.Commit();
}
return true;
}
catch (Exception ex)
{
Application.ShowAlertDialog("Error: " + ex.Message);
}
return true;
}
private void RevDesc_TextChanged(object sender, EventArgs e)
{
}
private void SubDate_TextChanged(object sender, EventArgs e)
{
}
}
}
i was not able to access the form data so i use this;
public static bool DoSomethingInDwg(Database db)
{
try
{
var Revision = new Revision();
Solved! Go to Solution.