Skip to content

Instantly share code, notes, and snippets.

@chuongmep
Last active April 5, 2022 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuongmep/518a9bef4f9fa3e70696cc20b3a957d1 to your computer and use it in GitHub Desktop.
Save chuongmep/518a9bef4f9fa3e70696cc20b3a957d1 to your computer and use it in GitHub Desktop.
[CommandMethod("CreateBox")]
public void cmdCreateBox()
{
var doc = AcApp.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
var db = doc.Database;
using (var tr = db.TransactionManager.StartTransaction())
{
var bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
var btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
var box = new Solid3d();
box.CreateBox(100, 200, 300);
var matrix = ed.CurrentUserCoordinateSystem;
matrix = matrix * Matrix3d.Displacement(new Vector3d(111, 222, 333));
box.TransformBy(matrix);
btr.AppendEntity(box);
tr.AddNewlyCreatedDBObject(box, true);
tr.Commit();
}
}
#Copyright(c) 2021 Hồ Văn Chương
# @chuongmep https://chuongmep.com/
import clr
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import os
import math
clr.AddReference('acmgd')
clr.AddReference('acdbmgd')
clr.AddReference('accoremgd')
# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *
doc = Application.DocumentManager.MdiActiveDocument
ed = doc.Editor
db = doc.Database
# Write Code Below
with doc.LockDocument():
with doc.Database as db:
with db.TransactionManager.StartTransaction() as t:
bt = t.GetObject(db.BlockTableId,OpenMode.ForRead)
btr = t.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite)
# Do action here
box = Solid3d()
box.CreateBox(1000,2000,3000)
matrix = ed.CurrentUserCoordinateSystem
matrix = matrix * Matrix3d.Displacement(Vector3d(111, 222, 333))
box.TransformBy(matrix)
btr.AppendEntity(box)
t.AddNewlyCreatedDBObject(box, True)
t.Commit()
print("Created Box")
[CommandMethod("AutoCADSamples", "CreateLine", CommandFlags.Modal)]
public void CreateLineCommand()
{
var document = Application.DocumentManager.MdiActiveDocument;
var database = document.Database;
var editor = document.Editor;
try
{
using (var transaction = database.TransactionManager.StartTransaction())
{
var blockTable = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);
var modelSpace =
(BlockTableRecord)
transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
var startPt = new Point3d(0.0, 0.0, 0.0);
var endPt = new Point3d(100.0, 100.0, 0.0);
var line = new Line(startPt, endPt);
modelSpace.AppendEntity(line);
transaction.AddNewlyCreatedDBObject(line, true);
transaction.Commit();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#Copyright(c) 2021, Hồ Văn Chương
# @chuongmep, https://chuongmep.com/
import clr
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import os
import math
clr.AddReference('acmgd')
clr.AddReference('acdbmgd')
clr.AddReference('accoremgd')
# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *
doc = Application.DocumentManager.MdiActiveDocument
ed = doc.Editor
db = doc.Database
#Code Here :
objects = []
p1 = Point3d(0,0,0)
p2 = Point3d(100,100,0)
line1 = Line(p1,p2)
with doc.LockDocument():
with doc.Database as db:
with db.TransactionManager.StartTransaction() as t:
bt = t.GetObject(db.BlockTableId,OpenMode.ForRead)
btr = t.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite)
btr.AppendEntity(line1)
t.AddNewlyCreatedDBObject(line1,True)
t.Commit()
print("Line Created")
[CommandMethod("AutoCADSamples", "CreatePolyLine", CommandFlags.Modal)]
public void CreatePolyLineCommand()
{
var document = Application.DocumentManager.MdiActiveDocument;
var database = document.Database;
try
{
using (var transaction = database.TransactionManager.StartTransaction())
{
var blockTable = (BlockTable)transaction.GetObject(database.BlockTableId, OpenMode.ForRead);
var modelSpace = (BlockTableRecord)transaction.GetObject(blockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
var polyLine = new Polyline();
polyLine.AddVertexAt(0, new Point2d(0.0, 0.0), 0.0, 0.0, 0.0);
polyLine.AddVertexAt(1, new Point2d(100.0, 0.0), 0.0, 0.0, 0.0);
polyLine.AddVertexAt(2, new Point2d(100.0, 100.0), 0.0, 0.0, 0.0);
polyLine.AddVertexAt(3, new Point2d(0.0, 100.0), 0.0, 0.0, 0.0);
polyLine.Closed = true;
modelSpace.AppendEntity(polyLine);
transaction.AddNewlyCreatedDBObject(polyLine, true);
transaction.Commit();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#Copyright(c) 2021, Hồ Văn Chương
# @chuongmep, https://chuongmep.com/
import clr
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import os
import math
clr.AddReference('acmgd')
clr.AddReference('acdbmgd')
clr.AddReference('accoremgd')
# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *
doc = Application.DocumentManager.MdiActiveDocument
ed = doc.Editor
db = doc.Database
# Write Code Below
with doc.LockDocument():
with doc.Database as db:
with db.TransactionManager.StartTransaction() as t:
bt = t.GetObject(db.BlockTableId,OpenMode.ForRead)
btr = t.GetObject(bt[BlockTableRecord.ModelSpace],OpenMode.ForWrite)
# Do action here
pl = Polyline()
pl.AddVertexAt(0,Point2d(0.0, 0.0), 0.0, 0.0, 0.0)
pl.AddVertexAt(1,Point2d(100.0, 0.0), 0.0, 0.0, 0.0)
pl.AddVertexAt(2,Point2d(100.0, 100.0), 0.0, 0.0, 0.0)
pl.AddVertexAt(3,Point2d(0.0, 100.0), 0.0, 0.0, 0.0)
btr.AppendEntity(pl)
t.AddNewlyCreatedDBObject(pl,True)
t.Commit()
[CommandMethod("DeleteEntity")]
public void cmdDeleteEntity()
{
var doc = AcApp.DocumentManager.MdiActiveDocument;
var ed = doc.Editor;
var db = doc.Database;
var result = ed.GetEntity("Please select an entity to delete");
if (result.Status != PromptStatus.OK) return;
using (var tr = db.TransactionManager.StartTransaction())
{
var obj = tr.GetObject(result.ObjectId, OpenMode.ForWrite);
obj.Erase();
tr.Commit();
}
}
#Copyright(c) 2021, Hồ Văn Chương
# @chuongmep, https://chuongmep.com/
import clr
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import os
import math
clr.AddReference('acmgd')
clr.AddReference('acdbmgd')
clr.AddReference('accoremgd')
# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *
doc = Application.DocumentManager.MdiActiveDocument
ed = doc.Editor
db = doc.Database
result = ed.GetEntity("Please Select Object To Delete")
if(result.Status==PromptStatus.OK): objId = result.ObjectId
#Code Here :
with doc.LockDocument():
with doc.Database as db:
with db.TransactionManager.StartTransaction() as t:
obj = t.GetObject(objId, OpenMode.ForWrite)
obj.Erase()
t.Commit()
print("Object Deleted")
#Copyright(c) 2021, Hồ Văn Chương
# @chuongmep, https://chuongmep.com/
import clr
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import os
import math
clr.AddReference('acmgd')
clr.AddReference('acdbmgd')
clr.AddReference('accoremgd')
# Import references from AutoCAD
from Autodesk.AutoCAD.Runtime import *
from Autodesk.AutoCAD.ApplicationServices import *
from Autodesk.AutoCAD.EditorInput import *
from Autodesk.AutoCAD.DatabaseServices import *
from Autodesk.AutoCAD.Geometry import *
doc = Application.DocumentManager.MdiActiveDocument
ed = doc.Editor
db = doc.Database
# Write Code Below
Application.ShowAlertDialog("An error occurred!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment