1
Hatching / Re: Create associative hatch
« on: March 26, 2013, 01:21:28 PM »
Thanks fixo! I'm tired of using the built in hatch functions... its automatic selection almost always crashes my session.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
// get centroid of polygon
namespace BCCGETCENTROIDCOMMAND
{
public class BCCGETCENTROIDCOM
{
[CommandMethod("BCC:CENTROID")]
public static void BCCGETCENTROID()
{
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
Editor ed = acDoc.Editor;
Transaction tr = acCurDb.TransactionManager.StartTransaction();
// Start a transaction
using (tr)
{
SelectionFilter sf = new SelectionFilter(new TypedValue[1] { new TypedValue((int)DxfCode.Start, "LWPolyline") });//<<<--- a selection filter to get text only
PromptSelectionResult objects = ed.GetSelection(sf);
if (objects.Status != PromptStatus.OK) return;
List<Polyline> polylines = new List<Polyline>();
foreach (ObjectId oid in objects.Value.GetObjectIds())
{
DBObject ent = tr.GetObject(oid, OpenMode.ForWrite);
Polyline p = ent as Polyline;
// add the polylines to the array
DBObjectCollection dbobjs = new DBObjectCollection();
dbobjs.Add(ent);
// create solid to get region and centroid
Solid3d Solid = new Solid3d();
Solid.Extrude(((Region)Region.CreateFromCurves(dbobjs)[0]), 1, 0);
Point2d centroid = new Point2d(Solid.MassProperties.Centroid.X, Solid.MassProperties.Centroid.Y);
Solid.Dispose();
BlockTable acBlkTbl = tr.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRec; acBlkTblRec = tr.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
DBPoint acPoint = new DBPoint(new Point3d(centroid.X, centroid.Y,0));
acBlkTblRec.AppendEntity(acPoint);
tr.AddNewlyCreatedDBObject(acPoint, true);
acCurDb.Pdmode = 99;
acCurDb.Pdsize = 1;
}
// Save the new objects to the database
tr.Commit();
}
}
}
}You may want to use command using API invoke method acedCmd
e.g. open desired block in block editor then perform this code
(it's not mine though)Code: [Select]// Replace "accore.dll" by "acad.exe" for AutoCAD versions prior to 2013
[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("acad.exe", EntryPoint = "acedCmd", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
extern static private int acedCmd(IntPtr resbuf);
[CommandMethod("offpl")]
public void cmdOffset()
{
ResultBuffer rb = new ResultBuffer();
// RTSTR = 5005
rb.Add(new TypedValue(5005, "_.OFFSET"));
// start the insert command
acedCmd(rb.UnmanagedObject);
bool quit = false;
// loop round while the insert command is active
while (!quit)
{
// see what commands are active
string cmdNames = (string)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("CMDNAMES");
// if the INSERT command is active
if (cmdNames.ToUpper().IndexOf("OFFSET") >= 0)
{
// then send a PAUSE to the command line
rb = new ResultBuffer();
// RTSTR = 5005 - send a user pause to the command line
rb.Add(new TypedValue(5005, "\\"));
acedCmd(rb.UnmanagedObject);
}
else
// otherwise quit
quit = true;
}
}