Programmatically change dimensions from .NET

Programmatically change dimensions from .NET

MarkvanDenMunckhof
Participant Participant
2,893 Views
4 Replies
Message 1 of 5

Programmatically change dimensions from .NET

MarkvanDenMunckhof
Participant
Participant

Hello,

 

I am working on a C# application to automate our drawing process.

One of the requirements is that we need to be able to programmatically change dimensions in a existing drawing.

 

For example we have a drawing with a simple rectangle with two dimensions for the width and the height. From within a C# application I want to be able to open the drawing and then change the dimensions with the ones that are inputted by the user in the C# application.

 

I searched on different forums for answers to accomplish this, but can’t get any good examples for this? Also I am wondering how I can distinguish the different dimensions in a drawing, I don’t want to ask the user to point out what the width-dimension is and what the height-dimension is in the drawing. Most of the drawings have also more dimensions, and we want to automate this as much as possible. Is it possible to name dimensions, and refer from code to them?

 

We are using Autocad 2014, and most of our drawings are simple 2D drawings.

 

Any help would be greatly appreciated.

 

Marcel

0 Likes
2,894 Views
4 Replies
Replies (4)
Message 2 of 5

moogalm
Autodesk Support
Autodesk Support

Hi there,

 

Here is small snippet code to change Dimension of any entity, in the example, I have simple line with an Aligned Dimension.

 [CommandMethod("CDDIM",CommandFlags.Session)]
        public void CDDIM()
        {
            
            DocumentCollection docs = Application.DocumentManager;
            Document doc =  docs.Open("C:\\Temp\\TestDim.dwg");
            Database db = doc.Database;
            Editor ed = doc.Editor;
            /*When command is registered as Session, need to Lock the document to gain access*/
            using (DocumentLock acLckDoc = doc.LockDocument())
            {
                using (Transaction t = db.TransactionManager.StartTransaction())
                {
                    BlockTableRecord btr = t.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as BlockTableRecord;
                    foreach (ObjectId oID in btr)
                    {
                        Dimension dim = t.GetObject(oID, OpenMode.ForWrite) as Dimension;
                        if (dim != null)
                        {
                            try
                            {
                                /*Change Dimension Text Value*/
                                dim.DimensionText = "2000.00";
                                t.Commit();
                            }
                            catch (TargetInvocationException ex)
                            {
                                string message = ex.Message;
                            }
                        }

                    }
                }
            }
        }
0 Likes
Message 3 of 5

Ed__Jobe
Mentor
Mentor

Rather than trying to change dimension text, can you use associative dimensions and change the rectangle's size? Then the dims will update automatically. If so, then you could use constraints to fix the rectangle's size by setting user parameters, e.g. Length and Width. However, I don't see a way to programatically update the parameters.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 4 of 5

Ed__Jobe
Mentor
Mentor

The user parameters can be scripted using -PARAMETERS.

 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 5 of 5

kgallagherCGHDN
Advocate
Advocate

this code returns an error "Operation is not valid due to the current state of the object" any thoughts?

0 Likes