Announcements

The Autodesk Community Forums has a new look. Read more about what's changed on the Community Announcements board.

Need Help with Batch Flatten Command

Anonymous

Need Help with Batch Flatten Command

Anonymous
Not applicable

Hello,

I've been trying to create a script that opens a dwg file, performs the flatten command, and saves a copy of the file with "_TOP" appended to the original file name. Right now I am just trying to get one file to work. I can get a file to open but cannot seem to call the flatten command before lDoc.Save(); I know from some research that SendStringToExecute runs async to the given process, but is there a way to force to to run synchronously with other functions?

 

Any help would be appreciated, thank you!

 

var lFileName = "C:\\Users\\KDhyne\\Desktop\\DWG Testing\\4821410SA2.dwg"
var lNewName = $"{Path.GetFileNameWithoutExtension(lFilePath)}_TOP.dwg";
var lDoc = ACAD.DocumentManager.Open(lFileName, false);
using (DocumentLock docLock = lDoc.LockDocument())
{
   lDoc.SendStringToExecute("zoom all ", true, false, true);
   Common.ExecuteString("flatten all Y ", true, false, true);
} 
lDoc.CloseAndSave(lNewName);
0 Likes
Reply
Accepted solutions (1)
1,272 Views
6 Replies
Replies (6)

ed57gmc
Mentor
Mentor

See if this thread helps.

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

Anonymous
Not applicable

Thank you Ed, that gave me a promising start, but I'm still having trouble. I've posted my revised code below. If I run as is, I get an eInvalidInput error. If I comment out the line with flatten, the command executes fine, saving a new copy of the file, but it does not zoom first. 

I must have missed something from the linked thread, so if you can help me figure it out I'd appreciate it!

Thanks!

 

[CommandMethod("SuperFlatten")]
        public static void SuperFlatten()
        {
            var lFileName = "C:\\Users\\KDhyne\\Desktop\\DWG Testing\\4821410SA2.dwg";
            var lNewName = $"C:\\Users\\KDhyne\\Desktop\\DWG Testing\\2D\\{Path.GetFileNameWithoutExtension(lFileName)}_TOP.dwg";
            var lDoc = ACAD.DocumentManager.Open(lFileName, false);

            using (DocumentLock docLock = lDoc.LockDocument())
            {
                lDoc.Editor.Command("_ZOOM", "All");
                lDoc.Editor.Command("FLATTEN", "ALL", "Y");

            }

            lDoc.Database.SaveAs(lNewName, Autodesk.AutoCAD.DatabaseServices.DwgVersion.Current);
            lDoc.CloseAndDiscard();
        }
0 Likes

ed57gmc
Mentor
Mentor

@Anonymous wrote:

Thank you Ed, that gave me a promising start, but I'm still having trouble. I've posted my revised code below. If I run as is, I get an eInvalidInput error. If I comment out the line with flatten, the command executes fine, saving a new copy of the file, but it does not zoom first. 

I must have missed something from the linked thread, so if you can help me figure it out I'd appreciate it!

Thanks!

 

[CommandMethod("SuperFlatten", CommandFlags.Session + CommandFlags.NoHistory)]
        public static void SuperFlatten()
        {
            var lFileName = "C:\\Users\\KDhyne\\Desktop\\DWG Testing\\4821410SA2.dwg";
            var lNewName = $"C:\\Users\\KDhyne\\Desktop\\DWG Testing\\2D\\{Path.GetFileNameWithoutExtension(lFileName)}_TOP.dwg";
            var lDoc = ACAD.DocumentManager.Open(lFileName, false);

            using (DocumentLock docLock = lDoc.LockDocument())
            {
                lDoc.Editor.Command("_ZOOM", "All");
                lDoc.Editor.Command("FLATTEN", "ALL", "Y");

            }

            lDoc.Database.SaveAs(lNewName, Autodesk.AutoCAD.DatabaseServices.DwgVersion.Current);
            lDoc.CloseAndDiscard();
        }

See comments in red.

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

_gile
Mentor
Mentor
Accepted solution

Hi,

 

First, FLATTEN is not a command, it's an Express Tools LISP function  and cannot be called with the Editor.Command() method.

As said in the topic linked by @ed57gmc, Editor.Command() can only be called in the Document context, so it cannot be used in a command decored with CommandFlags.Session.

In my opinion, if you want to batch process files using AutoCAD commands and AutoLISP function, you should try with accoreconsole and/or some batch scripting application as ScriptPro.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Anonymous
Not applicable

Thank you very much Gilles, I was able to create a very simple script and use ScriptPro to process an entire folder of drawings!

0 Likes

Kélcyo
Advocate
Advocate
Can you provide a code sample that runs Express Tools' "Flatten" and receives an object as an input parameter?
0 Likes