Message 1 of 14
Not applicable
07-22-2016
12:58 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi,
I have created some simple code which takes some blocks, draws a polyline (and sweeps a solid along it) from the block to a line and groups all the objects into a individually grouped name. The code is below and works perfectly for what I want when using the command line. What I want to do now is apply this is a form but I don't have much of an idea as to how to start.
I think I want to create a modal win form, and have created the design in VBE but I'm not sure how to connect the code to the buttons etc and then launch it within cad.
My CAD code is here
[CommandMethod("CBC")]
public static void ConnectBlocksToCurves()
{
var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
if (doc == null)
return;
var db = doc.Database;
var ed = doc.Editor;
// Get the block to connect
try{
//Filter list to only choose blocks
TypedValue[] filterlist = new TypedValue[1];
filterlist[0] = new TypedValue(0, "INSERT");
SelectionFilter filter = new SelectionFilter(filterlist);
PromptSelectionResult psr1 = ed.GetSelection(filter);
if (psr1.Status != PromptStatus.OK)
return;
SelectionSet psrSet = psr1.Value;
// Define gully depth
PromptDoubleOptions pdo = new PromptDoubleOptions("\nSelect gully depth to invert (mm)");
pdo.AllowArbitraryInput = true;
pdo.AllowNone = false;
pdo.Keywords.Add("600");
pdo.Keywords.Add("750");
pdo.UseDefaultValue = true;
pdo.Keywords.Default = "600";
PromptDoubleResult res = ed.GetDouble(pdo);
if (res.Status != PromptStatus.OK)
return;
double depth = res.Value / 1000; // Converts depth to m
// Get the curve to connect it to
var peo2 = new PromptEntityOptions("\nSelect the curve");
peo2.SetRejectMessage("\nMust be a curve.");
peo2.AddAllowedClass(typeof(Curve), false);
var per2 = ed.GetEntity(peo2);
if (per2.Status != PromptStatus.OK)
return;
//Specify connection pipe diameter
PromptDoubleOptions pdo1 = new PromptDoubleOptions("\nSpecify pipe diameter (mm)");
pdo1.AllowNone = false;
pdo1.AllowZero = false;
PromptDoubleResult pdr1 = ed.GetDouble(pdo1);
if (pdr1.Status != PromptStatus.OK)
return;
double dia = pdr1.Value / 1000;
var cId = per2.ObjectId;
ed.WriteMessage("\nFound {0} references. ", psrSet.Count);
using (var tr = db.TransactionManager.StartTransaction())
{
var c = tr.GetObject(cId, OpenMode.ForRead) as Curve;
var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
var btr = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
//Sort blocks based on distance along string
ObjectIdCollection ids = psr1.Value.GetObjectIds();
var sorted = new BlockReference[ids.Count];
for (int i = 0; i < ids.Count; i++)
{
sorted[i] = tr.GetObject(ids[i], OpenMode.ForRead) as BlockReference;
Polyline3d pl = new Polyline3d();
btr.AppendEntity(pl);
tr.ConnectBlockToCurve(btr, sorted[i], c, depth, pl);//Call extension to draw a temporary polyline from the block to the curve
//Create temp cirlc with which to sweep
Circle circ = new Circle();
circ.Center = pl.StartPoint;
circ.Diameter = dia;
//Create sweep options
SweepOptionsBuilder sob = new SweepOptionsBuilder();
//Sweep Alignment
sob.Align = SweepOptionsAlignOption.AlignSweepEntityToPath;
//Sweep base point
sob.BasePoint = pl.StartPoint;
//Profile rotates to folow path
sob.Bank = true;
Entity ent;
Solid3d sol = new Solid3d();
sol.CreateSweptSolid(
circ,
pl,
sob.ToSweepOptions()
);
ent = sol;
btr.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
pl.Erase(true);
// Create group and add items
//Check gd is empty or not
gd.UpgradeOpen();
Group grp = new Group("3D Drainage Elements", true);
string grpName = "Group" + i.ToString();
ObjectId grpID = gd.SetAt(grpName, grp);
tr.AddNewlyCreatedDBObject(grp, true);
grp.Append(sorted[i].ObjectId);
grp.Append(ent.ObjectId);
};
tr.Commit();
}
}catch(System.Exception ex){
System.Windows.Forms.MessageBox.Show(ex.Message,"Error");
}
}
My form is here:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void depthBox_TextChanged(object sender, EventArgs e)
{
}
private void blocks_Click(object sender, EventArgs e)
{
}
private void curves_Click(object sender, EventArgs e)
{
}
private void diaBox_TextChanged(object sender, EventArgs e)
{
}
private void go_Click(object sender, EventArgs e)
{
}
}
Thanks for reading.
Solved! Go to Solution.