.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Error inserting parallel lines with angle

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
josano
955 Views, 9 Replies

Error inserting parallel lines with angle

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();
            }
        }
    }
}

9 REPLIES 9
Message 2 of 10
Hallex
in reply to: josano

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.

http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer's%20Guide/index.html?url=WS1a919382...

(Type offset in the search window in there)

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 3 of 10
Bryco
in reply to: josano

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

Message 4 of 10
Irvin
in reply to: josano

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

Message 5 of 10
Jeffrey_H
in reply to: Irvin

Do not forget what Kerry told you abot using offset.

 

If you are using just line GetTransformedCopy********* does a shallow clone so use on basic entites.

 

And Mline and MlineStyle

You can also find your answers @ TheSwamp
Message 6 of 10
josano
in reply to: Jeffrey_H

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();
            }
        }
    }
}
Message 7 of 10
Bryco
in reply to: josano

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?

 

 

Message 8 of 10
sardeson
in reply to: Bryco

Bryco ... Off subject, but was unsure how to reach you regarding this old post vbAssoc

 

I'm attempting to use the vbAssoc as was wondering if I might contact you with any questions I may have that surface while attempting to use this powerful function.

 

Thanks,

Brian

Message 9 of 10
Bryco
in reply to: josano

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.

Message 10 of 10
Hallex
in reply to: josano

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);
        }

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost