Aller au contenu

Coordonnées espace objet vers espace papier


coyotte

Messages recommandés

Bonjour à tous,

 

je viens vers vous car j'ai un petit soucis de correspondance entre les coordonnées d'un point en espace objet que je voudrais faire correspondre au centre du viewport d'une présentation ceci quel que soit les scu de l'espace objet ou du viewport.

 

Quelqu'un pourrait il m'aiguiller.

 

Merci d'avance de votre aide.

http://www.carpetdiem.net

Plugins pour AutoCAD

Lien vers le commentaire
Partager sur d’autres sites

Salut,

 

Tu peux utiliser ces méthodes d'extensions extarites de GeometryExtension.

 

using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

namespace GeometryExtensions
{
   /// <summary>
   /// Provides extension methods for the Viewport type.
   /// </summary>
   public static class ViewportExtensions
   {
       /// <summary>
       /// Gets the transformation matrix from the specified model space viewport Display Coordinate System (DCS)
       /// to the World Coordinate System (WCS).
       /// </summary>
       /// <param name="vp">The instance to which this method applies.</param>
       /// <returns>The DCS to WDCS transformation matrix.</returns>
       public static Matrix3d DCS2WCS(this Viewport vp)
       {
           return
               Matrix3d.Rotation(-vp.TwistAngle, vp.ViewDirection, vp.ViewTarget) *
               Matrix3d.Displacement(vp.ViewTarget - Point3d.Origin) *
               Matrix3d.PlaneToWorld(vp.ViewDirection);
       }

       /// <summary>
       /// Gets the transformation matrix from the World Coordinate System (WCS)
       /// to the specified model space viewport Display Coordinate System (DCS).
       /// </summary>
       /// <param name="vp">The instance to which this method applies.</param>
       /// <returns>The WCS to DCS transformation matrix.</returns>
       public static Matrix3d WCS2DCS(this Viewport vp)
       {
           return vp.DCS2WCS().Inverse();
       }

       /// <summary>
       /// Gets the transformation matrix from the specified paper space viewport Display Coordinate System (DCS)
       /// to the paper space Display Coordinate System (PSDCS).
       /// </summary>
       /// <param name="vp">The instance to which this method applies.</param>
       /// <returns>The DCS to PSDCS transformation matrix.</returns>
       public static Matrix3d DCS2PSDCS(this Viewport vp)
       {
           return
               Matrix3d.Scaling(vp.CustomScale, vp.CenterPoint) *
               Matrix3d.Displacement(vp.CenterPoint.GetAsVector()) *
               Matrix3d.Displacement(vp.ViewCenter.Convert3d().GetAsVector().Negate());
       }

       /// <summary>
       /// Gets the transformation matrix from the Paper Space Display Coordinate System (PSDCS)
       /// to the specified paper space viewport Display Coordinate System (DCS). 
       /// </summary>
       /// <param name="vp">The instance to which this method applies.</param>
       /// <returns>The PSDCS to DCS transformation matrix.</returns>
       public static Matrix3d PSDCS2DCS(this Viewport vp)
       {
           return vp.DCS2PSDCS().Inverse();
       }   
       /// <summary>
       /// Converts a 2d point into a 3d point with Z coordinate equal to 0.
       /// </summary>
       /// <param name="pt">The instance to which the method applies.</param>
       /// <returns>The corresponding 3d point.</returns>
       public static Point3d Convert3d(this Point2d pt)
       {
           return new Point3d(pt.X, pt.Y, 0.0);
       }
   }
}

 

Un exemple d'utilisation : depuis une présentation, lancer la commande TEST qui crée un cercle dans l'espace objet, centre la vue de la fenêtre sur le centre du cercle et crée un point (nodal) sur le centre du cercle dans l'espace papier.

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using GeometryExtensions;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

[assembly: CommandClass(typeof(ViewportTranformation.Commands))]

namespace ViewportTranformation
{
   public class Commands
   {
       [CommandMethod("TEST", CommandFlags.NoTileMode)]
       public void Test()
       {
           Document doc = AcAp.DocumentManager.MdiActiveDocument;
           Database db = doc.Database;
           Editor ed = doc.Editor;

           PromptEntityOptions peo = new PromptEntityOptions("\nSélectionnez une fenêtre de présentation: ");
           peo.SetRejectMessage("Objet non valide.");
           peo.AddAllowedClass(typeof(Viewport), true);
           PromptEntityResult per = ed.GetEntity(peo);
           if (per.Status != PromptStatus.OK) 
               return;

           var wcsCenter = new Point3d(20.0, 10.0, 0.0);

           // créer un cercle dans l'espace objet
           using (Transaction tr = db.TransactionManager.StartTransaction())
           {
               var modelSpace = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
               using (var circle = new Circle(wcsCenter, Vector3d.ZAxis, 10.0))
               {
                   modelSpace.AppendEntity(circle);
                   tr.AddNewlyCreatedDBObject(circle, true);
               }
               tr.Commit();
           }

           // centre la vue de la fenêtre sur le cercle et créer un point au centre du cercle dans l'espace papier
           using (Transaction tr = db.TransactionManager.StartTransaction())
           {
               var vp = (Viewport)tr.GetObject(per.ObjectId, OpenMode.ForWrite);
               var dcsCenter = wcsCenter.TransformBy(vp.WCS2DCS());
               vp.ViewCenter = new Point2d(dcsCenter.X, dcsCenter.Y);
               
               var curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
               var psdcsCenter = wcsCenter.TransformBy(vp.DCS2PSDCS() * vp.WCS2DCS());
               // ou :
               // var psdcsCenter = dcsCenter.TransformBy(vp.DCS2PSDCS());
               using (var pt = new DBPoint(psdcsCenter))
               {
                   curSpace.AppendEntity(pt);
                   tr.AddNewlyCreatedDBObject(pt, true);
               }
               tr.Commit();
           }
       }
   }
}

Gilles Chanteau - gileCAD - GitHub
Développements sur mesure pour AutoCAD

Lien vers le commentaire
Partager sur d’autres sites

Créer un compte ou se connecter pour commenter

Vous devez être membre afin de pouvoir déposer un commentaire

Créer un compte

Créez un compte sur notre communauté. C’est facile !

Créer un nouveau compte

Se connecter

Vous avez déjà un compte ? Connectez-vous ici.

Connectez-vous maintenant
×
×
  • Créer...

Information importante

Nous avons placé des cookies sur votre appareil pour aider à améliorer ce site. Vous pouvez choisir d’ajuster vos paramètres de cookie, sinon nous supposerons que vous êtes d’accord pour continuer. Politique de confidentialité