using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System.Text.RegularExpressions;
namespace MyPlugin
{
public class MyCommands : IExtensionApplication
{
public void Initialize()
{
// Initialization logic
}
public void Terminate()
{
// Termination logic
}
[CommandMethod("CreateReinforcement")]
public void CreateReinforcementCommand()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
TypedValue[] filterList = new TypedValue[] { new TypedValue(0, "TEXT") };
SelectionFilter filter = new SelectionFilter(filterList);
PromptSelectionResult selRes = ed.GetSelection(filter);
if (selRes.Status == PromptStatus.OK)
{
SelectionSet selSet = selRes.Value;
foreach (SelectedObject selObj in selSet)
{
if (selObj is DBText text)
{
string textString = text.TextString;
if (Regex.IsMatch(textString, @"#\d+@\d+")) // مثال: تشخیص متن های شامل آرماتور (# و @ به عنوان جداکننده)
{
ReinforcementData data = ParseReinforcementData(textString);
if (data != null)
{
Point3d insertionPoint = text.Position;
DrawReinforcement(db, tr, insertionPoint, data);
}
}
}
}
}
tr.Commit();
}
}
private ReinforcementData ParseReinforcementData(string text)
{
// تابع برای تجزیه اطلاعات آرماتور از متن
// پیاده سازی این تابع به الگوی متن شما بستگی دارد
// مثال: "#10@200" -> قطر 10، فاصله 200
Match match = Regex.Match(text, @"#(\d+)@(\d+)");
if (match.Success)
{
int diameter = int.Parse(match.Groups[1].Value);
int spacing = int.Parse(match.Groups[2].Value);
return new ReinforcementData { Diameter = diameter, Spacing = spacing };
}
return null;
}
private void DrawReinforcement(Database db, Transaction tr, Point3d point, ReinforcementData data)
{
// تابع برای رسم آرماتور در نقطه مشخص شده با استفاده از اطلاعات
// پیاده سازی این تابع به نیازهای شما بستگی دارد
// مثال: رسم یک دایره
Circle circle = new Circle(point, db.Elevation, data.Diameter);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
btr.AppendEntity(circle);
tr.AddNewlyCreatedDBObject(circle, true);
}
}
public class ReinforcementData
{
public int Diameter { get; set; }
public int Spacing { get; set; }
}
}