How to install GSDML file via Siemens TIA openness API into TIA project?

1.3k views Asked by At

Updating with very useful info using guidance from mrsargent

I am trying to automate following steps in C# (Visual Studio) with following steps:

  • run and connect to TIA portal
  • create project
  • install GSDML device files
  • add PLC and single device as per GSDML
  • design application relationship between product and PLC (cpu)

I tried to use OpenNess Demo Application for the same but I am unable to step through the code and there is no option in the Demo GUI to install GSDML files in the same.

I tried to write the following code as per documentation for CAX import of GSDML file but faced errors as described below:

Code:

using 

(TiaPortal tiaPortal = new TiaPortal(TiaPortalMode.WithoutUserInterface))
{
    Console.WriteLine("TIA Portal has started");
    ProjectComposition projects = tiaPortal.Projects;
    Console.WriteLine("Opening Project...");        
    DirectoryInfo dinfo = new DirectoryInfo(@"C:\projects\TestProjects\");
    string unixTimestamp = Convert.ToString((int)DateTime.Now.Subtract(new DateTime(1970, 1, 1)).TotalSeconds);
    string prj_name = "Prj_" + unixTimestamp;
    Project project = null;
    try
    {
        project = projects.Create(dinfo, prj_name);
    }
    catch (Exception)
    {
        Console.WriteLine(String.Format("Could not open project {0}", projectPath.FullName));
        Console.WriteLine("Demo complete hit enter to exit");
        Console.ReadLine();
        return;
    }
                                
    CaxProvider caxProvider = project.GetService<CaxProvider>();
    if (caxProvider != null)
    {
        // GETTING ERROR OVER HERE
        // {"Error when calling method 'Import' of type 'Siemens.Engineering.Cax.CaxProvider'.\r\n\r\nThe path of the import file 'C:\\Gaurav\\GSDML-xxxxxxxx.xml' with the extension '.xml' is invalid.\r\n"}
        caxProvider.Import(
            new FileInfo(@"C:\GSDML-xxxx.xml"),
            new FileInfo(@"C:\ProjectImport_Log.log"), 
            CaxImportOptions.MoveToParkingLot
        );
    }

    Console.WriteLine(String.Format("Project {0} is open", project.Path.FullName));
    // IterateThroughDevices(project);
    project.Close();
    Console.WriteLine("Demo complete hit enter to exit");
    Console.ReadLine();
}

Following error is observed:

{"Error when calling method 'Import' of type 'Siemens.Engineering.Cax.CaxProvider'.\r\n\r\nThe path of the import file 'C:\GSDML-xxx.xml' with the extension '.xml' is invalid.\r\n"}

1

There are 1 answers

6
mrsargent On BEST ANSWER

Yes this is a difficult thing to do. However it is possible. First you need proper documentation that is a little difficult to find. The manual is very detailed and good found here

You need the import the GSD file as a CAx that is found page 939 of the documentation.

//Access the CaxProvider service
Project project = tiaPortal.Projects.Open(...);
CaxProvider caxProvider = project.GetService<CaxProvider>();
if(caxProvider != null)
{ 
  // Perform Cax export and import operation
}

To create this CAx (an xml document) you need to look starting at page 988 of this manual. It will tell you how to configure. It is far too much to explain and list in this forum but the documentation does a good job of explaining after you read it 5 times ;)

It is probably best to read this entire import/export section in order to get a full understanding of how to do this. Hope this helps!