2

I would like to add a new environment to the Geoprocessor Environments from an ArcMap.net Add in. I try adding the env to the GPEnvironmentManager with the following code but when ArcMap opens and I look at the environments, its not there.

            IGPEnvironment secTolEnv = new GPEnvironmentClass();
            secTolEnv.Category = "MyCategory";
            secTolEnv.Name = "_sectiontolerance";
            secTolEnv.DisplayName = "Section Tolerance";
            secTolEnv.DataType = new GPLinearUnitTypeClass();
            secTolEnv.Value = new GPLinearUnitClass
            {
                Value = 360,
                Units = esriUnits.esriFeet
            } as IGPValue;

            IGPEnvironmentManager GPEnvManager = new GPEnvironmentManagerClass();
            GPEnvManager.PersistAll = true;
            GPEnvManager.AddEnvironment(secTolEnv);

I am calling this while initializing the add in extension. Its getting called when ArcMap opens and I can step through it fine.

Is this possible?

I was thinking maybe I need to get the GPEnvironmentManager from ArcMap itself but I have not figured out how to do this yet.

2

Have a look here, use the SetEnvironment from the IGeoProcessor2 object in the same manner as doing it in python (sort of), their example is gp.SetEnvironmentValue("workspace", "C:\data\saltlake.gdb") where gp is a variable of type IGeoProcessor2.

| improve this answer | |
  • I want to add a new environment, not set an existing one. This only allows modifying existing environments. Thanks though. – TurboGus May 28 '14 at 22:04
  • So you've created a geoprocessing tool that will consume the _SelectionTolerance geoprocessing environment? Why not just use system.environment? msdn.microsoft.com/en-us/library/z46c489x(v=vs.110).aspx and get msdn.microsoft.com/en-us/library/77zkk0b6(v=vs.110).aspx – Michael Stimson May 28 '14 at 22:14
  • We have a custom geoprocessing tool that creates and uses this custom environment. It works well. Now we want to mimic that experience with an Add in so we have constant products. The problem with the system.envrionment is that it does not provide the user with the same accessibility. We could create a UI for it but we would much rather have it in the GP Environments so that the user can go to one place to make environmental changes. – TurboGus May 28 '14 at 22:23
  • 1
    I'm not sure that you can just add an environment setting and have it come up in the ArcGis geoprocessing environments property page... try adding it to an existing category like XY Resolution and Tolerance; if it doesn't come up then you will need to implement your own property page. – Michael Stimson May 28 '14 at 22:46
2

We created a helper class to handle it for us then we call it from an Add-in Extension constructor.

public static class CVEnvironmentManager
{
    public const string EnvironmentsCategory = "CV";
    public static IGPEnvironmentManager gpApplicationEnv;

    static CVEnvironmentManager()
    {
        IGPComHelper gpComHelper = new GpDispatchClass();
        gpApplicationEnv = gpComHelper.EnvironmentManager;

        //find the parent enviroment manager
        while (gpApplicationEnv.Parent != null)
        {
            gpApplicationEnv = gpApplicationEnv.Parent;
        }

    }

    public static void CreateEnvironments()
    {
        //Section Tolerance Environment
        IGPEnvironment secTolEnv = new GPEnvironmentClass();
        secTolEnv.Category = EnvironmentsCategory;
        secTolEnv.Name = string.Format("{0}_{1}", EnvironmentsCategory, "sectionTolerance");
        secTolEnv.DisplayName = "Section Tolerance";
        secTolEnv.DataType = new GPLinearUnitTypeClass();
        secTolEnv.Value = new GPLinearUnitClass { Value = 360, Units = esriUnits.esriFeet } as IGPValue;
        AddEnvironment(secTolEnv);

    }

    private static void AddEnvironment(IGPEnvironment pGPEnvironment)
    {
        try
        {
            gpApplicationEnv.FindEnvironment(pGPEnvironment.Name);
        }
        catch (Exception ex)
        {
            //environment not found
            gpApplicationEnv.AddEnvironment(pGPEnvironment);
        }
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.