Ejemplo n.º 1
0
        public static void AlignBlockCommand()
        {
            var options = new PromptEntityOptions("\nSelect polyline: ");

            options.SetRejectMessage("Objet non valide.");
            options.AddAllowedClass(typeof(Polyline), true);
            var result = Quick.Editor.GetEntity(options);

            if (result.Status != PromptStatus.OK)
            {
                return;
            }
            var lineId = result.ObjectId;

            options.Message = "\nSelect block: ";
            options.RemoveAllowedClass(typeof(Polyline));
            options.AddAllowedClass(typeof(BlockReference), true);
            result = Quick.Editor.GetEntity(options);
            if (result.Status != PromptStatus.OK)
            {
                return;
            }
            var blockId = result.ObjectId;
            var db      = lineId.Database;

            using (var trans = db.TransactionManager.StartTransaction()) {
                try {
                    var line          = trans.GetObject(lineId, OpenMode.ForRead) as Polyline;
                    var blockRef      = trans.GetObject(blockId, OpenMode.ForWrite) as BlockReference;
                    var blockpos      = blockRef.Position;
                    var pointOverLine = line.GetClosestPointTo(blockRef.Position, false);
                    blockRef.Position = pointOverLine; // move
                    //angel between block to the nearest point
                    var b2near = blockpos.Convert2d(new Plane()).GetVectorTo(pointOverLine.Convert2d(new Plane())).Angle *Rad2Deg;
                    blockRef.Rotation = (b2near - 90) * Deg2Rad; //-90 to convert to block plane (0 is downwards).
                    trans.Commit();
                } catch (Exception ex) {
                    Quick.WriteLine(ex.Message + "\n" + ex.StackTrace);
                }
            }
        }