using System;
using System.IO;
using System.Reflection;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using BDuct_Global;
namespace BDuct
{
public class AutoCADFunctions
{
public static void InsertBlock(string BlkName)
{
Editor ed = GlobalVariables.ed;
Database db = GlobalVariables.db;
using (var DocLock = GlobalVariables.doc.LockDocument())
{
// First transaction: To add the block definition
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
if (!bt.Has(BlkName))
{
// Path to the block file
string assemblyPath = Assembly.GetExecutingAssembly().Location;
string directoryPath = Path.GetDirectoryName(assemblyPath);
string blockFilePath = Path.Combine(directoryPath, "Blocks", BlkName + ".dwg");
if (!File.Exists(blockFilePath))
{
ed.WriteMessage("\nBlock file does not exist: " + blockFilePath);
return; // Exit if file does not exist
}
bt.UpgradeOpen();
using (Database tempDb
= new Database
(false,
true)) {
tempDb.ReadDwgFile(blockFilePath, FileShare.Read, true, null);
ObjectIdCollection blockIds
= new ObjectIdCollection
(); // blockIds.Add(tempDb.BlockTableId);
var Id = db.Insert(BlkName, tempDb, true);
db
.WblockCloneObjects(blockIds, bt
.Id,
new IdMapping
(), DuplicateRecordCloning
.Replace,
false); }
bt.DowngradeOpen();
tr.Commit();
}
}
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
if (bt.Has(BlkName))
{
bt.UpgradeOpen();
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlkName], Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead);
BlockReference temp_ent
= new BlockReference
(new Point3d
(0,
0,
0), btr
.ObjectId); BlockIns Jigger_Ins
= new BlockIns
(temp_ent
); PromptResult pr_Ins = ed.Drag(Jigger_Ins);
if (pr_Ins.Status == PromptStatus.OK)
{
BlockRot Jigger_Rot
= new BlockRot
(temp_ent
); PromptResult pr_Rot = ed.Drag(Jigger_Rot);
if (pr_Rot.Status == PromptStatus.OK)
{
BlockTableRecord currentSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForWrite);
currentSpace.AppendEntity(temp_ent);
tr.AddNewlyCreatedDBObject(temp_ent, true);
temp_ent.Erase();
BlockReference ent
= new BlockReference
(new Point3d
(0,
0,
0), btr
.ObjectId); currentSpace.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
ent.Position = Jigger_Ins.position;
ent.Rotation = Jigger_Rot.rotation;
DynamicBlockReferencePropertyCollection props = ent.DynamicBlockReferencePropertyCollection;
foreach (DynamicBlockReferenceProperty prop in props)
{
if (!prop.ReadOnly) // Replace "YourDynamicProperty"
{
switch (prop.PropertyName)
{
case "DUCT_1":
prop.Value = Jigger_Rot.distance;
break;
case "DUCT_WIDTH":
prop.Value = GlobalVariables.Duct_Width;
break;
case "DUCT_HEIGHT":
//prop.Value = GlobalVariables.Duct_Hee
break;
case "INSULATION":
prop.Value = GlobalVariables.Insulation_Type;
break;
case "INSULATION_INTERNAL":
prop.Value = GlobalVariables.Insulation_Internal_Size;
break;
case "INSULATION_EXTERNAL":
prop.Value = GlobalVariables.Insulation_External_Size;
break;
default:
break;
}
}
}
bt.DowngradeOpen(); tr.Commit();
}
else
{
bt.DowngradeOpen(); tr.Abort();
}
}
else
{
bt.DowngradeOpen(); tr.Abort();
}
}
}
}
}
}
public class BlockRot : EntityJig
{
#region Fields
private double mRotation;
private double mDistance;
#endregion
public BlockRot(Entity ent) : base(ent)
{
}
// Add a property to access mPosition
public double rotation
{
get { return mRotation; }
set { mRotation = value; }
}
public double distance
{
get { return mDistance; }
set { mDistance = value; }
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
Point3d basePoint = (Entity as BlockReference).Position;
JigPromptPointOptions prOptionsPoint
= new JigPromptPointOptions
("\rSelect next point: "); prOptionsPoint.BasePoint = basePoint;
prOptionsPoint.UseBasePoint = true;
prOptionsPoint.Cursor = CursorType.RubberBand;
PromptPointResult promptPointResult = prompts.AcquirePoint(prOptionsPoint);
if (promptPointResult.Status == PromptStatus.Cancel)
{
return SamplerStatus.Cancel;
}
else
{
Point3d newPosition = promptPointResult.Value;
double newRotation = CalculateRotation(basePoint, newPosition);
double newDistance = basePoint.DistanceTo(newPosition);
if (!newDistance.Equals(mDistance) || !newRotation.Equals(mRotation))
{
mDistance = newDistance;
mRotation = newRotation;
(Entity as BlockReference).Rotation = mRotation;
UpdateDynamicBlockProperty(mDistance); // Update distance dynamically
return SamplerStatus.OK;
}
}
return SamplerStatus.NoChange;
}
protected override bool Update()
{
(Entity as BlockReference).Rotation = mRotation;
return true;
}
#region Methods
private double CalculateRotation(Point3d basePoint, Point3d newPosition)
{
Vector3d vector = newPosition - basePoint;
// Assuming the X-axis as the reference, adjust as needed
double angle = Math.Atan2(vector.Y, vector.X);
return angle;
}
private void UpdateDynamicBlockProperty(double distance)
{
// Assuming the block reference and its dynamic properties are accessible
// Similar to the previous example, directly set the dynamic block property here
DynamicBlockReferencePropertyCollection props = (Entity as BlockReference).DynamicBlockReferencePropertyCollection;
foreach (DynamicBlockReferenceProperty prop in props)
{
if (!prop.ReadOnly) // Replace "YourDynamicProperty"
{
switch (prop.PropertyName)
{
case "DUCT_1":
prop.Value = distance;
break;
case "DUCT_WIDTH":
prop.Value = GlobalVariables.Duct_Width;
break;
case "DUCT_HEIGHT":
//prop.Value = GlobalVariables.Duct_Hee
break;
case "INSULATION":
prop.Value = GlobalVariables.Insulation_Type;
break;
case "INSULATION_INTERNAL":
prop.Value = GlobalVariables.Insulation_Internal_Size;
break;
case "INSULATION_EXTERNAL":
prop.Value = GlobalVariables.Insulation_External_Size;
break;
default:
break;
}
}
}
}
#endregion
}
public class BlockIns : EntityJig
{
#region Fields
private Point3d mPosition;
#endregion
public BlockIns(BlockReference ent) : base(ent)
{
}
// Add a property to access mPosition
public Point3d position
{
get { return mPosition; }
set { mPosition = value; }
}
protected override SamplerStatus Sampler(JigPrompts prompts)
{
JigPromptPointOptions prOptions1
= new JigPromptPointOptions
("\rSelect start point:"); PromptPointResult prResult1 = prompts.AcquirePoint(prOptions1);
if (prResult1.Status == PromptStatus.Cancel) return SamplerStatus.Cancel;
if (prResult1.Value.Equals(mPosition))
{
return SamplerStatus.NoChange;
}
else
{
mPosition = prResult1.Value;
UpdateDynamicBlockProperty(15); // Update distance dynamically
return SamplerStatus.OK;
}
}
protected override bool Update()
{
(Entity as BlockReference).Position = mPosition;
return true;
}
private void UpdateDynamicBlockProperty(double distance)
{
// Assuming the block reference and its dynamic properties are accessible
// Similar to the previous example, directly set the dynamic block property here
DynamicBlockReferencePropertyCollection props = (Entity as BlockReference).DynamicBlockReferencePropertyCollection;
foreach (DynamicBlockReferenceProperty prop in props)
{
if (!prop.ReadOnly) // Replace "YourDynamicProperty"
{
switch (prop.PropertyName)
{
case "DUCT_1":
prop.Value = distance;
break;
case "DUCT_WIDTH":
prop.Value = GlobalVariables.Duct_Width;
break;
case "DUCT_HEIGHT":
//prop.Value = GlobalVariables.Duct_Hee
break;
case "INSULATION":
prop.Value = GlobalVariables.Insulation_Type;
break;
case "INSULATION_INTERNAL":
prop.Value = GlobalVariables.Insulation_Internal_Size;
break;
case "INSULATION_EXTERNAL":
prop.Value = GlobalVariables.Insulation_External_Size;
break;
default:
break;
}
}
}
}
}
}