Adding the numeric contents in the next text
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
namespace YourAutoCADPluginNamespace
{
public class TextProcessingCommands
{
[CommandMethod("ExtractAndSumText")]
public void ExtractAndSumText()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptSelectionResult selRes = ed.GetSelection();
if (selRes.Status != PromptStatus.OK)
{
ed.WriteMessage("\nError in getting selection.");
return;
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
double totalCfm = 0.0;
SelectionSet selSet = selRes.Value;
foreach (SelectedObject selObj in selSet)
{
if (selObj.ObjectId.ObjectClass.Name != "AcDbText")
continue;
DBText txt = tr.GetObject(selObj.ObjectId, OpenMode.ForRead) as DBText;
if (txt != null)
{
string textContents = txt.TextString;
string[] splitText = textContents.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string item in splitText)
{
double numericValue;
if (double.TryParse(item, out numericValue))
{
totalCfm += numericValue;
}
}
}
}
// Create new text entity with the total CFM
Point3d insertPt = new Point3d(0, 0, 0); // You need to define the insertion point
using (DBText totalTxt = new DBText())
{
totalTxt.TextString = totalCfm.ToString();
totalTxt.Position = insertPt;
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
btr.AppendEntity(totalTxt);
tr.AddNewlyCreatedDBObject(totalTxt, true);
}
tr.Commit();
}
}
}
i am having above code to create the automate calculate the total cfm.
Example ; 1000 CFM and 2000 CFM = 3000 CFM
}