Question-and-Answer Resource for the Building Energy Modeling Community
Get started with the Help page
Ask Your Question
6

Sorting a macro-generated IDF

asked 2014-10-17 02:06:14 -0600

updated 2015-07-11 17:17:18 -0600

Is there a script available from the command line which can take an IDF created by EPMacro (or by hand, Python, etc.) and sort it into the "normal" order as produced by the IDF Editor when choosing the "sorted" order when saving the file?

edit retag flag offensive close merge delete

Comments

A good question indeed. So far I've always opened it in the IDF Editor, changed to version to something else, changed it back to what it was, and used "save" (you can't save it if you didn't change something). It would be a plus to write an extra line of code in my script in order to have it done automatically so I get it sorted, and to re-add the descriptive comments for each line item (you don't have it with Eppy)

Julien Marrec's avatar Julien Marrec  ( 2014-10-17 02:27:56 -0600 )edit

If no other solutions come forward today soon then I'll write something for my current project then port it into Eppy. Looks like I can use the format exported by Help > Create objectList.txt as a template.

Jamie Bull's avatar Jamie Bull  ( 2014-10-17 03:29:42 -0600 )edit
1

I agree with Annie and Julien. IDF editor change, change back and save is the method I have used to to get a sorted IDF file.

This is an untested guess but I believe the code being used to do the sort is this file available on Github: https://github.com/NREL/EnergyPlus/bl...

If you are looking for the IDF format and order I think that would be in the IDD file for each respective version. I do think an IDF "cleaner" could be valuable. Jamie let us know if you make it. Or NREL guys, if you know of a quicker path that could be used chime in.

jhtravis's avatar jhtravis  ( 2014-10-23 14:30:41 -0600 )edit

5 Answers

Sort by ยป oldest newest most voted
3

answered 2020-01-07 16:37:47 -0600

updated 2020-01-07 16:46:09 -0600

Simply save the IDF using eppy, it sorts automatically

        idf = IDF(idfpath)
        idf.saveas(idfpath)

Here's an entire script to sort all the IDF files that are in the same folder as the python file:

       from eppy import modeleditor
       from eppy.modeleditor import IDF
       import os
       iddfile = "C:/EnergyPlusV9-0-1/Energy+.idd"
       IDF.setiddname(iddfile)
       dir = os.path.dirname(os.path.realpath(__file__))
       for path, subdirs, files in os.walk(dir):
           for name in files:
               ext = os.path.splitext(name)[-1].lower()
               if ext == ".idf":
                   idfpath = os.path.join(path, name)
                   idf = IDF(idfpath)
                   idf.saveas(idfpath)
edit flag offensive delete link more
7

answered 2014-11-19 05:01:22 -0600

updated 2014-12-18 11:29:10 -0600

This is not the ideal solution, but it's what has worked for me at the moment. I'm using Python to automate the process of launching the IDF Editor, opening an IDF file, changing the version number, saving the file (thus achieving the desired sorting), changing the version number back, saving again[, repeating if required until all files are sorted], and closing the IDF Editor.

When run from a folder it loops through all the IDF files in the folder and sorts them before closing the IDF Editor. This might mean you run into memory problems when sorting hundreds of files - I haven't tried it yet so don't know what the limits are - but it would be easy enough to adapt to take batches of say ten or twenty at a time.

I also had a look at pywinauto but ran into problems finding the right place to enter values in the IDF Editor GUI. If anyone has any tips on that please let me know.

Hope this helps someone out.

"""
idf_editor.sorter
~~~~~~~~~~~~~~~~~

A simple script which controls the IDF Editor application, taking advantage of
the fact that it tidies up the file when saving.

It would be better if this could happen in the background but the simplest way
to control IDF Editor from Python is using SendKeys which requires the window
to be in the foreground.

This script should be run from the folder containing the IDF files to be sorted.
Filenames will be unaffected.

"""

import os
import time

import win32com.client

EPLUS_VERSION = '8.1'  # The EnergyPlus version number of the IDFs to be sorted 
IDF_EDITOR_PATH = "C:\EnergyPlusV8-1-0\PreProcess\IDFEditor\IDFEditor.exe"

shell = win32com.client.DispatchEx("WScript.Shell")
shell.Run(IDF_EDITOR_PATH)
shell.AppActivate("IDF Editor")
time.sleep(1)  # Wait for the IDF Editor to open - you may need to increase this

def sort_idf(idf_name):
    shell.SendKeys("^o")
    time.sleep(1)  # Wait for dialog to open - you may be able to reduce this
    shell.SendKeys(idf_name + "{ENTER}")
    shell.SendKeys("{TAB}" * 8)
    shell.SendKeys("{F2}XX{ENTER}^s")  # This is where the sorting occurs
    shell.SendKeys("{F2}%s{ENTER}^s" % EPLUS_VERSION)  # Write the correct value

idfs = (f for f in os.listdir('.') if os.path.splitext(f)[1] == '.idf')
for idf in idfs:
    sort_idf(idf)

shell.SendKeys("%{F4}")
edit flag offensive delete link more

Comments

1

Nice, I suggest you add python to your tags for this question.

MatthewSteen's avatar MatthewSteen  ( 2014-11-19 08:00:38 -0600 )edit

Jamie, I can't figure out why it won't run on my machine. It's sending the keys to the cmd prompt before the IDF editor is opened I guess. Does it work with time.sleep(1) for you?

Julien Marrec's avatar Julien Marrec  ( 2014-11-19 11:49:18 -0600 )edit

It does, but I'm running it on a very fast server. Try changing it to a longer wait, as that probably is the problem.

Jamie Bull's avatar Jamie Bull  ( 2014-11-19 12:09:57 -0600 )edit
3

answered 2014-10-17 09:00:39 -0600

Similar to the above comment, when we add to our EnergyPlus files outside the idf, all we do is open the idf, add a new object, delete it, make sure the idf editor is on ordered save, save it and everything is nicely ordered again. Not the neatest solution but easy enough.

edit flag offensive delete link more

Comments

I think that's supposed to be a comment and not an answer

Julien Marrec's avatar Julien Marrec  ( 2014-10-24 07:26:33 -0600 )edit

Certainly not easy when you have several hundred IDFs to sort, which is why I'm really hoping to avoid having to do it.

Jamie Bull's avatar Jamie Bull  ( 2014-11-18 11:33:56 -0600 )edit
2

answered 2014-11-21 08:46:29 -0600

updated 2014-11-21 08:47:34 -0600

You could do this with OpenStudio Idf classes. Specifically OpenStudio::Workspace. Something like...

require 'openstudio'
ws = OpenStudio::Workspace::load('/path/to/input.idf').get
ws.order.setOrderByIddEnum
ws.save '/path/to/output.idf'
edit flag offensive delete link more
1

answered 2019-12-16 22:50:41 -0600

mldichter's avatar

I modified the script a bit so that the IDF Editor is opened and closed for every file being sorted. This prevents crashing on my machine.

WARNING!!!
This script is dangerous. One of the steps in this script is opening a new IDF file from inside the IDF Editor. I almost messed up a bunch of IDF files because the Open File window defaults to the last directory that an IDF was opened in. You need to make sure the default directory is the correct directory.
If you don't understand what I'm talking about, then don't run this script. If you're paranoid, run it in a virtual machine.
image description

"""
idf_editor_sorter.py
~~~~~~~~~~~~~~~~~

A simple script which controls the IDF Editor application, taking advantage of
the fact that it tidies up the file when saving.

It would be better if this could happen in the background but the simplest way
to control IDF Editor from Python is using SendKeys which requires the window
to be in the foreground.

This script should be run from the folder containing the IDF files to be sorted.
Filenames will be unaffected.

"""

############################ WARNING ###################################
#The Open File window defaults to the directory of the last opened file.
#Be cautious of where the files that are being modified are.
#If you're not sure what that means, then DON'T RUN this script!!!
#https://unmethours.com/question/535/sorting-a-macro-generated-idf/

import os
import time

import win32com.client

EPLUS_VERSION = '9.2'  # The EnergyPlus version number of the IDFs to be sorted 
IDF_EDITOR_PATH = "C:\EnergyPlusV9-2-0\PreProcess\IDFEditor\IDFEditor.exe"

def sort_idf(idf_name):
    wait_time = 2
    shell = win32com.client.DispatchEx("WScript.Shell")
    time.sleep(wait_time)
    shell.Run(IDF_EDITOR_PATH)
    time.sleep(wait_time)
    shell.AppActivate("IDF Editor")
    time.sleep(wait_time)  # Wait for the IDF Editor to open - you may need to increase this
    shell.SendKeys("^o")
    time.sleep(wait_time)  # Wait for dialog to open - you may be able to reduce this
    shell.SendKeys(idf_name + "{ENTER}")
    time.sleep(wait_time)
    shell.SendKeys("{TAB}" * 8)
    time.sleep(wait_time)
    shell.SendKeys("{F2}XX{ENTER}^s")  # This is where the sorting occurs
    time.sleep(wait_time)
    shell.SendKeys("{F2}%s{ENTER}^s" % EPLUS_VERSION)  # Write the correct value
    time.sleep(wait_time)
    shell.SendKeys("%{F4}")
    time.sleep(wait_time)

idfs = (f for f in os.listdir('.') if os.path.splitext(f)[1] == '.idf')
for idf in idfs:
    sort_idf(idf)

WARNING!!!
This script is dangerous. One of the steps in this script is opening a new IDF file from inside the IDF Editor. I almost messed up a bunch of IDF files because the Open File window defaults to the last directory that an IDF was opened in. You need to make sure the default directory is the correct directory.
If you don't understand what I'm talking about, then don't run this script. If you're paranoid, run it in a virtual machine.
image description

edit flag offensive delete link more

Comments

2

Two cents: track your IDF files with version control (as you should) using Git for eg. That way you can always revert changes if you don't like them. Also, try to pass an absolute path instead of just idf_name. sort_idf(os.path.abspath(idf))

Julien Marrec's avatar Julien Marrec  ( 2019-12-17 03:26:40 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Training Workshops

Careers

Question Tools

2 followers

Stats

Asked: 2014-10-17 02:06:14 -0600

Seen: 1,007 times

Last updated: Jan 07 '20