Friends,
I made some modifications to the code with the tips.
I can now insert with angle but for some reason the values are only correct when I insert horizontally.
Below the code changed, some of you know where I could be wrong?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcDb = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
namespace ClassLibrary9
{
public class Class1
{
[CommandMethod("ssa")]
public static void GetPointsFromUser()
{
// le o banco de dados corrente e inicia o gerenciador de transações
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
PromptPointResult pPtRes;
PromptPointOptions pPtOpts = new PromptPointOptions("");
// Mensagem para ponto inicial
pPtOpts.Message = "\nSelecione o ponto inicial da linha: ";
pPtRes = acDoc.Editor.GetPoint(pPtOpts);
Matrix3d UCS = acDoc.Editor.CurrentUserCoordinateSystem;
Point3d ptStart = pPtRes.Value.TransformBy(UCS);
// Se o usuario pressionar esc ou cancelar o comando sai
if (pPtRes.Status == PromptStatus.Cancel) return;
// Mensagem para ponto final
pPtOpts.Message = "\nSelecione o ponto final da linha: ";
pPtOpts.UseBasePoint = true;
pPtOpts.BasePoint = ptStart;
pPtRes = acDoc.Editor.GetPoint(pPtOpts);
Point3d ptEnd = pPtRes.Value;
if (pPtRes.Status == PromptStatus.Cancel) return;
// Inicia a transação
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl;
BlockTableRecord acBlkTblRec;
// Abre o model space para escrever
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
PromptAngleOptions angOpts = new PromptAngleOptions("\nSelecione o angulo...");
angOpts.BasePoint = ptStart;
PromptDoubleResult angRes = acDoc.Editor.GetAngle(angOpts);
Line acline1 = new Line(ptStart, ptEnd);
acline1.Layer = "CT4";
Line acline2 = new Line(ptStart, ptEnd);
acline2.Layer = "TR2";
Line acline3 = new Line(ptStart, ptEnd);
acline3.Layer = "CT4";
acline2.StartPoint = new Point3d(acline2.StartPoint.X , acline2.StartPoint.Y - 44.7, 0);
acline2.EndPoint = new Point3d(acline2.EndPoint.X , acline2.EndPoint.Y - 44.7, 0);
acline3.StartPoint = new Point3d(acline3.StartPoint.X , acline3.StartPoint.Y - 51, 0);
acline3.EndPoint = new Point3d(acline3.EndPoint.X , acline3.EndPoint.Y - 51, 0);
acline1.SetDatabaseDefaults();
acline2.SetDatabaseDefaults();
acline3.SetDatabaseDefaults();
// Adicionar as linhas ao desenho
acBlkTblRec.AppendEntity(acline1);
acBlkTblRec.AppendEntity(acline2);
acBlkTblRec.AppendEntity(acline3);
acTrans.AddNewlyCreatedDBObject(acline1, true);
acTrans.AddNewlyCreatedDBObject(acline2, true);
acTrans.AddNewlyCreatedDBObject(acline3, true);
// Confirma as alterações
acTrans.Commit();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcDb = Autodesk.AutoCAD.DatabaseServices.TransactionManager;
namespace ClassLibrary9
{
public class Class1
{
[CommandMethod("ssa")]
public static void GetPointsFromUser()
{
// le o banco de dados corrente e inicia o gerenciador de transações
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
PromptPointResult pPtRes;
PromptPointOptions pPtOpts = new PromptPointOptions("");
// Mensagem para ponto inicial
pPtOpts.Message = "\nSelecione o ponto inicial da linha: ";
pPtRes = acDoc.Editor.GetPoint(pPtOpts);
Matrix3d UCS = acDoc.Editor.CurrentUserCoordinateSystem;
Point3d ptStart = pPtRes.Value.TransformBy(UCS);
// Se o usuario pressionar esc ou cancelar o comando sai
if (pPtRes.Status == PromptStatus.Cancel) return;
// Mensagem para ponto final
pPtOpts.Message = "\nSelecione o ponto final da linha: ";
pPtOpts.UseBasePoint = true;
pPtOpts.BasePoint = ptStart;
pPtRes = acDoc.Editor.GetPoint(pPtOpts);
Point3d ptEnd = pPtRes.Value;
if (pPtRes.Status == PromptStatus.Cancel) return;
// Inicia a transação
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl;
BlockTableRecord acBlkTblRec;
// Abre o model space para escrever
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
PromptAngleOptions angOpts = new PromptAngleOptions("\nSelecione o angulo...");
angOpts.BasePoint = ptStart;
PromptDoubleResult angRes = acDoc.Editor.GetAngle(angOpts);
Line acline1 = new Line(ptStart, ptEnd);
acline1.Layer = "CT4";
Line acline2 = new Line(ptStart, ptEnd);
acline2.Layer = "TR2";
Line acline3 = new Line(ptStart, ptEnd);
acline3.Layer = "CT4";
acline2.StartPoint = new Point3d(acline2.StartPoint.X , acline2.StartPoint.Y - 44.7, 0);
acline2.EndPoint = new Point3d(acline2.EndPoint.X , acline2.EndPoint.Y - 44.7, 0);
acline3.StartPoint = new Point3d(acline3.StartPoint.X , acline3.StartPoint.Y - 51, 0);
acline3.EndPoint = new Point3d(acline3.EndPoint.X , acline3.EndPoint.Y - 51, 0);
acline1.SetDatabaseDefaults();
acline2.SetDatabaseDefaults();
acline3.SetDatabaseDefaults();
// Adicionar as linhas ao desenho
acBlkTblRec.AppendEntity(acline1);
acBlkTblRec.AppendEntity(acline2);
acBlkTblRec.AppendEntity(acline3);
acTrans.AddNewlyCreatedDBObject(acline1, true);
acTrans.AddNewlyCreatedDBObject(acline2, true);
acTrans.AddNewlyCreatedDBObject(acline3, true);
// Confirma as alterações
acTrans.Commit();
}
}
}
}