using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using Autodesk.AutoCAD.ApplicationServices;
using acApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using static latihanCAD.function;
namespace latihanCAD
{
public class hapuslagi
{
[CommandMethod("xx")]
public static void CreateHatchedBlock()
{
var doc = acApp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
var ed = doc.Editor;
var objCol = new DBObjectCollection();
var ListPoints = new List<KeyValuePair<ObjectId, double>>();
var pso2 = new PromptSelectionOptions(); SelectionFilter sfilter = null;
//SELECT FOR BLOCK REFERENCE
PromptSelectionResult ss2 = null;
pso2.MessageForAdding = "\nSelect Block Reference <Click After Select>:";
sfilter = new SelectionFilter(new[] { new TypedValue(0, "INSERT") });
ss2 = ed.GetSelection(pso2, sfilter);
if (ss2.Status != PromptStatus.OK)
{
MessageBox.Show("\nThere is not block reference to select :!");
return;
}
using (Transaction trans = db.TransactionManager.StartTransaction())
{
var btr = trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
foreach (ObjectId id in ss2.Value.GetObjectIds())
{
var ent = (Entity)id.GetObject(OpenMode.ForRead);
var br = ent as BlockReference;
if (ent is BlockReference)
{
double ky = 0.0;
ObjectId myid = ObjectId.Null;
Curve curve = null;
ent.Explode(objCol);
foreach (Object obj in objCol)
{
// ed.WriteMessage("\n{0}", obj.GetType());
if (obj is Curve)
{
var crv1 = obj as Curve;
if (crv1.Area > 0)
{
if (ky < crv1.Area)
{
ky = crv1.Area;
myid = crv1.ObjectId;
curve = crv1;
}
}
}
}
if (ky > 0)
{
var ext1 = new Extents3d();
var cloneCurve = curve.Clone() as Entity;
cloneCurve.ColorIndex = 3;
ext1 = cloneCurve.GeometricExtents;
btr.AppendEntity(cloneCurve);
trans.AddNewlyCreatedDBObject(cloneCurve, true);
ed.WriteMessage("\nid clone curve " + cloneCurve.ObjectId.ToString());
cloneCurve.Dispose();
}
}
}
ed.WriteMessage("\ncount block reference " + ss2.Value.Count.ToString());
trans.Commit();
}
}
}
}
[CommandMethod("bref1")]
public static void beref()
{
var doc = acApp.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
Database db = acApp.DocumentManager.MdiActiveDocument.Database;
var objCol = new DBObjectCollection();
var ListPoints = new List<KeyValuePair<Curve, double>>();
var ss2 = ed.GetSelection();
using (Transaction trans = db.TransactionManager.StartTransaction())
{
ObjectId ModelSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(db);
BlockTableRecord model = trans.GetObject(ModelSpaceId,OpenMode.ForWrite) as BlockTableRecord;
foreach (ObjectId id in ss2.Value.GetObjectIds())
{
var ent = (Entity)id.GetObject(OpenMode.ForRead);
if (ent is BlockReference)
{
var br = ent.ObjectId.GetObject(OpenMode.ForRead) as BlockReference;
Extents3d e3d;
try
{
e3d = br.GeometryExtentsBestFit();
Point3d cnt = new Point3d (((e3d.MinPoint.X + e3d.MaxPoint.X) / 2)-0.05, (e3d.MinPoint.Y + e3d.MaxPoint.Y) / 2, (e3d.MinPoint.Z + e3d.MaxPoint.Z) / 2);
DBObjectCollection collection =
ed.TraceBoundary(cnt, true);
foreach (DBObject obj in collection)
{
Entity ent1 = obj as Entity;
if (ent1 != null)
{
//make the color as red.
ent1.ColorIndex = 3;
model.AppendEntity(ent1);
trans.AddNewlyCreatedDBObject(ent1, true);
}
}
}
catch (System.Exception)
{
e3d = br.GeometricExtents;
}
//ed.WriteMessage("\n point extend blockref " + e3d.ToString());
}
}
trans.Commit();
}
}
[CommandMethod("xx")]
public static void xxCommand()
{
var doc = acApp.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
//SELECT FOR BLOCK REFERENCE
var pso = new PromptSelectionOptions();
pso.MessageForAdding = "\nSelect Block Reference <Click After Select>:";
var filter = new SelectionFilter(new[] { new TypedValue(0, "INSERT") });
var psr = ed.GetSelection(pso, filter);
if (psr.Status != PromptStatus.OK)
{
acApp.ShowAlertDialog("None block reference selected !");
return;
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
var btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
foreach (ObjectId id in psr.Value.GetObjectIds())
{
var br = (BlockReference)id.GetObject(OpenMode.ForRead);
var objCol = new DBObjectCollection();
br.Explode(objCol);
objCol
.Cast<DBObject>()
.Select(p => ((Curve)tr.GetObject(p.ObjectId, OpenMode.ForRead)))
.Where(x => x.Area > 0)
.OrderByDescending(z => z.Area);
var curve = objCol[0] as Curve;
AddLayer("Layerhelper", 0, 82,false);
curve.ColorIndex = 253;
btr.AppendEntity(curve);
tr.AddNewlyCreatedDBObject(curve, true);
}
tr.Commit();
AddLayer("0", 0, 0, false);
}
}