Jump to content

Recommended Posts

Posted

I ask DEEPSEEK :
Can you make please for me app for autocad wich enables the STRETCH command to stretch dynamic blocks when the crossing windows catch the dynamic parameter grips. Supported parameters are Point parameters, Polar parameters, and Linear parameters, XY parameter
It provide me this code : 

I compile it , try it but it does not work !
May be somebody have a solutions for STRETCHING DYNAMIC BLOCKS like in this app :
Stretch app
Thanks in advance !!!

Quote

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;

namespace DynamicBlockStretch
{
    public class StretchHandler : IExtensionApplication
    {
        public void Initialize()
        {
            Application.DocumentManager.MdiActiveDocument.CommandWillStart += OnCommandWillStart;
        }

        public void Terminate() { }

        private void OnCommandWillStart(object sender, CommandEventArgs e)
        {
            if (e.GlobalCommandName == "STRETCH")
            {
                e.Veto();
                CustomStretch();
            }
        }

        private void CustomStretch()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

            try
            {
                // Select objects using crossing window
                PromptSelectionResult psr = ed.GetSelection(new PromptSelectionOptions(), new SelectionFilter());
                if (psr.Status != PromptStatus.OK) return;

                // Get stretch points
                PromptPointResult pprBase = ed.GetPoint("\nSpecify base point: ");
                if (pprBase.Status != PromptStatus.OK) return;
                Point3d basePoint = pprBase.Value;

                PromptPointResult pprDisplacement = ed.GetPoint("\nSpecify second point: ", new PromptPointOptions { UseBasePoint = true, BasePoint = basePoint });
                if (pprDisplacement.Status != PromptStatus.OK) return;
                Vector3d displacement = pprDisplacement.Value - basePoint;

                // Process each selected object
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    foreach (SelectedObject so in psr.Value)
                    {
                        Entity ent = tr.GetObject(so.ObjectId, OpenMode.ForRead) as Entity;
                        if (ent is BlockReference br && br.IsDynamicBlock)
                        {
                            ProcessDynamicBlock(br, displacement, tr);
                        }
                        // Else, handle regular entities here if needed
                    }
                    tr.Commit();
                }
            }
            catch (Exception ex)
            {
                ed.WriteMessage($"\nError: {ex.Message}");
            }
        }

        private void ProcessDynamicBlock(BlockReference br, Vector3d displacement, Transaction tr)
        {
            BlockTableRecord btr = tr.GetObject(br.DynamicBlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
            if (btr == null) return;

            foreach (DynamicBlockReferenceProperty prop in br.DynamicBlockReferencePropertyCollection)
            {
                Parameter param = btr.Parameters.Cast<Parameter>().FirstOrDefault(p => p.Name == prop.PropertyName);
                if (param == null) continue;

                br.UpgradeOpen();
                AdjustParameter(prop, param, displacement);
            }
        }

        private void AdjustParameter(DynamicBlockReferenceProperty prop, Parameter param, Vector3d displacement)
        {
            switch (param)
            {
                case LinearParameter linearParam:
                    AdjustLinearParameter(prop, linearParam, displacement);
                    break;
                case PointParameter pointParam:
                    AdjustPointParameter(prop, displacement);
                    break;
                // Extend with cases for PolarParameter, XYParameter, etc.
            }
        }

        private void AdjustLinearParameter(DynamicBlockReferenceProperty prop, LinearParameter linearParam, Vector3d displacement)
        {
            Vector3d direction = (linearParam.EndPoint - linearParam.StartPoint).GetNormal();
            double delta = displacement.DotProduct(direction);
            prop.Value = (double)prop.Value + delta;
        }

        private void AdjustPointParameter(DynamicBlockReferenceProperty prop, Vector3d displacement)
        {
            Point3d current = (Point3d)prop.Value;
            prop.Value = current + displacement;
        }
    }
}

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...