Voraussetzung
-
Die Anwendung ist mit TIA Portal über TIA Portal Openness verbunden.
Siehe Verbindung zum TIA Portal aufbauen -
Ein Projekt ist geöffnet
Siehe Projekt öffnen -
Eine Unit ist erstellt
Siehe Mit Software-Units arbeiten
Einleitung
Sie können mit TIA Portal Openness Units als Masterkopien in die Projektbibliothek und in die globale Bibliothek kopieren.
Mit Units als Masterkopie können Sie folgende Aktionen durchführen, während Sie TIA Portal Openness verwenden:
-
Eine Unit als Masterkopie in der Projektbibliothek aus Software-Units erstellen
-
Eine Unit als Masterkopie in der globalen Bibliothek aus Software-Units erstellen
-
Eine Unit aus einer Masterkopie in der Projektbibliothek in Software-Units neu erstellen
-
Eine Unit aus einer Masterkopie in der globalen Bibliothek in Software-Units neu erstellen
Programmcode
Ändern Sie folgenden Programmcode, um eine neue Unit als Masterkopie in der Projektbibliothek aus Software-Units mit Hilfe von MasterCopyComposition zu erstellen.
|
private void IMasterCopySource CreateUnitAsMastercopyInProjectLibrary(PlcUnitProvider m_UnitProvider, string plcUnitName = "Unit_1") { PlcUnitProvider m_UnitProvider = plc.GetService<PlcUnitProvider>(); PlcUnitComposition m_UnitComposition = m_UnitProvider.UnitGroup.Units; PlcUnit m_SoftwareUnit1 = unitComposition.Create("Unit_1");//Assuming existing units IMasterCopySource m_UnitAsMasterCopy = (IMasterCopySource)m_SoftwareUnit1;//Assuming that m_SoftwareUnit1 is present in the PLC m_ProjectLibrary.MasterCopyFolder.MasterCopies.Create(m_UnitAsMasterCopy); } |
Ändern Sie folgenden Programmcode, um eine Unit als Masterkopie in der globalen Bibliothek aus Software-Units zu erstellen.
|
private void IMasterCopySource CreateUnitAsMasterCopyInGlobalLibrary(PlcUnit m_SoftwareUnit1, TiaPortal m_TiaPortal) { // ... IMasterCopySource m_UnitAsMasterCopy = (IMasterCopySource)m_SoftwareUnit1;//Assuming that m_SoftwareUnit1 is present in the PLC m_TiaPortal.GlobalLibraries.Open(libfile, OpenMode.ReadWrite); GlobalLibrary m_GlobalLibrary = m_TiaPortal.GlobalLibraries[0]; m_GlobalLibrary.MasterCopyFolder.MasterCopies.Create(m_UnitAsMasterCopy) // ... } |
Wenn versucht wird, eine neue Unit in der schreibgeschützten globalen Bibliothek zu erstellen, wird eine Fehlermeldung ausgegeben. Eine wiederherstellbare Ausnahme wird mit der Meldung "Schreiben in schreibgeschützte Bibliotheken nicht möglich" ausgelöst.
|
private void IMasterCopySource CreateUnitAsMasterCopyInGlobalLibrary(PlcUnit m_SoftwareUnit1, TiaPortal m_TiaPortal) { IMasterCopySource m_UnitAsMasterCopy = (IMasterCopySource)m_SoftwareUnit1;//Assuming that m_SoftwareUnit1 is present in the PLC m_TiaPortal.GlobalLibraries.Open(libfile, OpenMode.ReadOnly); GlobalLibrary m_GlobalLibrary = m_TiaPortal.GlobalLibraries[0]; try { m_GlobalLibrary.MasterCopyFolder.MasterCopies.Create(m_UnitAsMasterCopy); } catch (Exception e) { Console.WriteLine(e.Message); } } |
Ändern Sie folgenden Programmcode, um eine Unit aus einer Masterkopie in der Projektbibliothek in Software-Units in PNV neu zu erstellen:
|
private void RecreateUnitFromProjectLibrary(Project project, PlcSoftware software, string plcUnitName = "Unit_2") { //... ProjectLibrary m_ProjectLibrary = project.ProjectLibrary; PlcUnitProvider m_UnitProvider = software.GetService<PlcUnitProvider>(); PlcUnitComposition m_UnitComposition = m_UnitProvider.UnitGroup.Units; // ... MasterCopy mc_Unit_2 = m_ProjectLibrary.MasterCopyFolder.MasterCopies.Find(plcUnitName); m_UnitComposition.CreateFrom(mc_Unit_2);//Recreate a Unit from Project Library to SoftwareUnits folder } |
Ändern Sie folgenden Programmcode, um eine Unit aus einer Masterkopie in der globalen Bibliothek in Software-Units in PNV neu zu erstellen:
|
private void RecreateUnitFromGlobalLibrary(TiaPorta m_TiaPortal, string plcUnitName = "Unit_2") { // ... GlobalLibrary m_GlobalLibrary = m_TiaPortal.GlobalLibraries[0]; // ... mc_Unit_2 = m_GlobalLibrary.MasterCopyFolder.MasterCopies.Find("Unit_2"); m_UnitComposition.CreateFrom(mc_Unit_2);//Recreate a unit from Global Library to SoftwareUnits folder } |