I´m trying to use a config file to adjust print settings but I have some trouble at understanding how this config stuff works, although I have looked up the code of other pyRevit tools. I want to toggle if the PDF scripts use Raster or Vector processing.
I have made a Settings pushbutton that will toggle the following script:
from pyrevit import forms
from pyrevit import script
user_config = script.get_config()
user_config.hidden_lines_processing = "Vector"
processing = user_config.get_option('hidden_lines_processing')
script.save_config()
option = forms.ask_for_one_item(['Vector', 'Raster'],default = processing,prompt='Select Hidden Lines Processing',title='Print Settings')
if option != processing:
user_config.hidden_lines_processing = option
script.save_config()
So my code is creating the parameter “hidden_lines_processing” every time, because if it does not exist i get an error. Is there any way to check if a parameter exists in the config.ini file?
Also i would like to know what this “default value” parameter is for that is used in the pyRevit docs?
>>> from pyrevit.userconfig import user_config
>>> user_config.add_section('newsection')
>>> user_config.newsection.property = value
>>> user_config.newsection.get('property', default_value)
>>> user_config.save_changes()
At last I would like to know how I can call the config parameter value from another script. Because I want to get parameter value from the Settingsconfic in multiple scripts, all the PDF scripts.
Still pretty confused by all these methods that are available to store data and I can not find out how to access data from another script with pyrevit methods.
So I built a method to read the .ini file myselfe, not very beautiful…can`t even find a method to get the path of the ini file. But hey, it works…
import os
# Get the user's AppData\Roaming directory
appdata_path = os.path.join(os.path.expanduser('~'), 'AppData', 'Roaming')
# Construct the full path to the .ini file within the pyRevit directory
config_file_path = os.path.join(appdata_path, 'pyRevit', 'pyRevit_config.ini')
def read_ini(filepath, section):
values = {}
with open(filepath, 'r') as f:
lines = f.readlines()
in_section = False
for line in lines:
line = line.strip()
if line.startswith('[') and line.endswith(']'):
in_section = line[1:-1] == section
elif in_section and '=' in line:
key, value = line.split('=')
values[key.strip()] = value.strip()
return values
config_values = read_ini(config_file_path, 'Settingsconfig')
print(config_values['hidden_lines_processing'])
Heres the explanaition for the default value. For me not important anymore because I can not use the .get method.
user_config.newsection.get('property', default_value): This retrieves the value of the property named property from the newsection section. If the property doesn’t exist, it returns the default_value instead.
So, the default_value is a value you determine to be the return value in case the property you’re trying to get doesn’t exist in the configuration. This can be helpful in cases where you don’t want your script to fail if a specific configuration property hasn’t been set yet. Instead of throwing an error, it can use this default value to continue its operations.
I’m sorry I’m late, but let me clarify some things:
The get_config function allows you to specify a section to access, and it will create a new one for you if it doesn’t exist. If you don’t specify the section, it will use a section usong the command name and the “config” postfix; this is why you see the “Settingsconfig” section and other commands cannot access it.
PyRevitConfigSectionParser.get_option is like the dict.get method, so you can read the value if it exist or use the default if it doesn’t.
I think that the examples are confusing since they condense the setting of an option (that can be done via attributes) with the reading, but you don’t need it to set it first if you want to retrieve the stored value!