Friends,
Can any of you help me with the code below?
I can insert a bracket of 51 mm x 6.3 mm horizontally.
If I try to enter angle values are not correct.
How can I leave the values prevail even with angle?
Hugs,
Josano
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);
Point3d ptStart = pPtRes.Value;
// 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;
Line acline1 = new Line(ptStart, ptEnd);
Line acline2 = new Line(ptStart, ptEnd);
Line acline3 = new Line(ptStart, ptEnd);
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();
}
}
}
}
Solved! Go to Solution.
Solved by Hallex. Go to Solution.
Working good in A2009
Try to set ORTHOMODE system variable to 1 before
And also, better yet to use offset in this case
IMO
See docs about
I.e.
(Type offset in the search window in there)
~'J'~
net makes the use of vectors very easy. You can make a new vector3d by subtacting the strtpoint from the endpoint, then use getnormal() to mormalize it (unit vector) then multiply the vector by the required distance. now add that vector to your endpoint
I'm not an expert but doens't AutoCAD needs radians for an angle.
Try to convert the angle using this code?
pseudocode:
angle = Convert.ToDouble(45) * Math.PI / 180; 45 is the angle given by the user.
I don't know this helps but give it a try.
Kind regards
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?
You either need 2 points or 1 point and an angle.
Then use some math to establish the end points.
What are you doing with the angle?
Sure, if I can. Since it uses lisp and I don't it may get a little tricky, I think a post in the vba section of the swamp would get you some help.
Try this instead
[CommandMethod("parlines")] 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; Editor ed = acDoc.Editor; try { PromptPointResult pPtRes; PromptPointOptions pPtOpts = new PromptPointOptions(""); // Mensagem para ponto inicial. pPtOpts.Message = "\nSelecione o ponto inicial da linha: "; pPtRes = acDoc.Editor.GetPoint(pPtOpts); if (pPtRes.Status == PromptStatus.Cancel) return; Matrix3d UCS = acDoc.Editor.CurrentUserCoordinateSystem; Point3d ptStart = pPtRes.Value.TransformBy(UCS); // Se o usuario pressionar esc ou cancelar o comando sai. // Mensagem para ponto final. pPtOpts.Message = "\nSelecione o ponto final da linha: "; pPtOpts.UseBasePoint = true; pPtOpts.BasePoint = ptStart; pPtRes = acDoc.Editor.GetPoint(pPtOpts); if (pPtRes.Status == PromptStatus.Cancel) return; Point3d ptEnd = pPtRes.Value.TransformBy(UCS); // 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(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord; Line acline1 = new Line(ptStart, ptEnd); double ang = acline1.Angle;//<-- angulo acline1.Layer = "CT4"; Line acline2 = new Line(ptStart, ptEnd); acline2.Layer = "TR2"; Line acline3 = new Line(ptStart, ptEnd); acline3.Layer = "CT4"; acline2.StartPoint = PolarPoint(acline2.StartPoint, ang - Math.PI / 2, 44.7); acline2.EndPoint = PolarPoint(acline2.EndPoint, ang - Math.PI / 2, 44.7); acline3.StartPoint = PolarPoint(acline2.StartPoint, ang - Math.PI / 2, 51.0); acline3.EndPoint = PolarPoint(acline2.EndPoint, ang - Math.PI / 2, 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(); } } catch (Autodesk.AutoCAD.Runtime.Exception ex) { ed.WriteMessage(string.Format( "Error: {0}\nTrace: {1}", ex.Message, ex.StackTrace)); } } // by Tony Tanzillo public static Point3d PolarPoint(Point3d basepoint, double angle, double distance) { return new Point3d( basepoint.X + (distance * Math.Cos(angle)), basepoint.Y + (distance * Math.Sin(angle)), basepoint.Z); }