protected ITcSmTreeItem CreateHardware(IWorker worker, bool scanHardware, bool simulation) { OrderScriptContext context = (OrderScriptContext)_context; ConfigurationInfo configurationInfo = context.Order.ConfigurationInfo; HardwareInfo hardware = configurationInfo.Hardware; ITcSmTreeItem devices = systemManager.LookupTreeItem("TIID"); // Getting IO-Configuration ITcSmTreeItem device = null; if (worker.CancellationPending) { throw new Exception("Execution cancelled!"); } // Scans the Fieldbus interfaces and adds an EtherCAT Device. string deviceName = "EtherCAT Master"; worker.ProgressStatus = string.Format("Creating device '{0}'", deviceName); device = Helper.CreateEthernetDevice(this.systemManager, DeviceType.EtherCAT_DirectMode, deviceName, worker); ITcSmTreeItem parent = device; foreach (BoxInfo boxInfo in hardware.Boxes) { if (worker.CancellationPending) { throw new Exception("Execution cancelled!"); } ITcSmTreeItem box = CreateBox(parent, boxInfo, worker); } return(device); }
/// <summary> /// Handling the event of the generate button. /// It generates a new TwinCat configuration /// </summary> private void _BgeneratePLC_Click(object sender, EventArgs e) { //this.Open(); //Debug.Assert(this._factory == null); try { OrderInfo order = this._orders[this._fbNumber]; // selects just the first item in the order.xml. TODO: connection with the AML SUC. Script script = ScriptLoader.GetScript(order.ConfigurationInfo.Script); // get the script under AvailableConfiguration of the order item previously selected if (script == null) { throw new ApplicationException(string.Format("Script '{0}' not found. Cannot start execution!", order.ConfigurationInfo.Script)); } _runningScript = script; // setting the running script with the one previously selected VsFactory fact = new VsFactory(); // VS factory to create the DTE Object and determine the VS version to integrate with TC if (_runningScript is ScriptEarlyBound) { this._factory = new EarlyBoundFactory(fact); } else if (_runningScript is ScriptLateBound) { this._factory = new LateBoundFactory(fact); } if (this._factory == null) { throw new ApplicationException("Generator not found!"); } OrderScriptContext context = new OrderScriptContext(this._factory, order); // we need to set the context of the script _worker = new ScriptBackgroundWorker(/*this._factory,*/ _runningScript, context); // worker for asynchronous script execution // Showing TwinCat UI and keeping it open after the creation of the code _factory.IsIdeVisible = true; _factory.IsIdeUserControl = true; _factory.SuppressUI = true; // Script execution: In Workerthread.cs it does OnDoWork() which has the main functions: Initialization, Execution and Cleanup _worker.BeginScriptExecution(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error"); } }
protected void CreateMotion(IWorker worker) { if (worker.CancellationPending) { throw new Exception("Execution cancelled!"); } OrderScriptContext context = (OrderScriptContext)_context; ConfigurationInfo configurationInfo = context.Order.ConfigurationInfo; ITcSmTreeItem ncConfig = systemManager.LookupTreeItem("TINC"); foreach (TaskInfo taskInfo in configurationInfo.MotionTasks) { ITcSmTreeItem task = null; worker.ProgressStatus = string.Format("Creating Motion Task '{0}'", taskInfo.Name); if (!TryLookupChild(ncConfig, taskInfo.Name, out task)) { task = ncConfig.CreateChild(taskInfo.Name, 1); } ITcSmTreeItem axes = null; TryLookupChild(task, "Axes", out axes); if (axes == null) { TryLookupChild(task, "Achsen", out axes); } foreach (AxisInfo axisInfo in taskInfo.Axes) { if (worker.CancellationPending) { throw new Exception("Execution cancelled!"); } ITcSmTreeItem axis = null; worker.ProgressStatus = string.Format("Creating Axis '{0}'", axisInfo.Name); if (!TryLookupChild(axes, axisInfo.Name, out axis)) { axis = axes.CreateChild(axisInfo.Name, 1); ConsumeTemplate(axis, axisInfo.TemplatePath); } } } }
protected void CreateMappings(IWorker worker) { OrderScriptContext context = (OrderScriptContext)_context; ConfigurationInfo configurationInfo = context.Order.ConfigurationInfo; MappingsInfo info = configurationInfo.Mappings; if (info != null) { if (worker.CancellationPending) { throw new Exception("Execution cancelled!"); } worker.ProgressStatus = "Generating mappings ..."; ConsumeMappings(info.TemplatePath); } }
/// <summary> /// Durch die Hauptschnittstelle ITcSysManager können wir ein neue PLCProjekt generieren. Die Lookup-Methode ist implementiert. /// </summary> protected ITcSmTreeItem CreatePlcProject(IWorker worker) { if (worker.CancellationPending) { throw new Exception("Execution cancelled!"); } OrderScriptContext context = (OrderScriptContext)_context; ConfigurationInfo configurationInfo = context.Order.ConfigurationInfo; string plcProjectName = configurationInfo.PlcProjectName; ITcSmTreeItem plcConfig = systemManager.LookupTreeItem("TIPC"); //Navigation im PLC struktur im TC. worker.ProgressStatus = string.Format("Creating empty PLC Project '{0}' ...", plcProjectName); ITcSmTreeItem plcProjectRoot = plcConfig.CreateChild(plcProjectName, 0, "", vsXaePlcEmptyTemplateName); //diese Method ausführen, um ein neues PLCProjekt erstellen. ITcPlcProject plcProjectRootIec = (ITcPlcProject)plcProjectRoot; //setting the properties of the PLC Project like boot option. plcProjectRootIec.BootProjectAutostart = true; plcProjectRootIec.GenerateBootProject(true); ITcSmTreeItem plcProject = plcProjectRoot.LookupChild(plcProjectName + " Project"); // for example Schulung and Schulung Project. Now the ITcTreeItem pointer is pointing Schulung Project foreach (PlcObjectInfo plcObjectInfo in context.Order.ConfigurationInfo.PlcObjects) //Creating all the components of the PLC Project { if (worker.CancellationPending) { throw new Exception("Execution cancelled!"); } switch (plcObjectInfo.Type) // we can have library, placeholder, POU, ITf, GVL and so on... { case PlcObjectType.DataType: // PlcObjectType defined in ScriptInfo. createWorksheet((WorksheetInfo)plcObjectInfo, plcProject, worker); break; case PlcObjectType.Library: createLibrary((LibraryInfo)plcObjectInfo, plcProject, worker); break; case PlcObjectType.Placeholder: createPlaceholder((PlaceholderInfo)plcObjectInfo, plcProject, worker); break; case PlcObjectType.POU: // first create folder and then create POU createWorksheet((WorksheetInfo)plcObjectInfo, plcProject, worker); break; case PlcObjectType.Itf: createWorksheet((WorksheetInfo)plcObjectInfo, plcProject, worker); break; case PlcObjectType.Gvl: createWorksheet((WorksheetInfo)plcObjectInfo, plcProject, worker); break; default: Debug.Fail(""); break; } } ITcSmTreeItem realtimeTasks = systemManager.LookupTreeItem("TIRT"); //creating tasks ITcSmTreeItem rtTask = realtimeTasks.CreateChild("PlcTask", TreeItemType.Task.AsInt32()); ITcSmTreeItem taskRef = null; worker.ProgressStatus = "Linking PLC instance with task 'PlcTask' ..."; if (!TryLookupChild(plcProject, "PlcTask", out taskRef)) { if (worker.CancellationPending) { throw new Exception("Execution cancelled!"); } taskRef = plcProject.CreateChild("PlcTask", TreeItemType.PlcTask.AsInt32(), "", "MAIN"); } //foreach (ITcSmTreeItem prog in taskRef) //{ // string name = prog.Name; //} //ErrorItems errors; //if (worker.CancellationPending) // throw new Exception("Execution cancelled!"); //bool ok = CompileProject(worker, out errors); //if (!ok) // throw new ApplicationException(string.Format("Plc Project compile produced '{0}' errors. Please see Visual Studio Error Window for details.!", plcProject.Name)); return(plcProject); }