/// <summary> /// Refreshes the project collections with the 'entitiesToRefreshFrom' list. /// If 'entitiesToRefreshFrom' has new entities they will be added to the project collection. /// If 'entitiesToRefresh' has entities that don't exist in 'entitiesToRefreshFrom' they will be removed. /// </summary> /// <param name="entitiesToRefresh">All PMEntities in the project.</param> /// <param name="entitiesToRefreshFrom">All the names of the entities that must exist on the project collections.</param> /// <param name="type">The type of the entity.</param> private void RefreshEntities(List <PMEntity> entitiesToRefresh, List <string> entitiesToRefreshFrom, string type) { var currentEntities = new List <PMEntity>(); foreach (string name in entitiesToRefreshFrom) { var originalEntity = entitiesToRefresh.SingleOrDefault(x => x.Name.ToUpperInvariant() == name.ToUpperInvariant()); if (originalEntity != null) { //Entity is already in the project collections currentEntities.Add(originalEntity); } else { PMEntity entity = null; entity = PMEntityFactory.CreateEntity(_powerMILL, type, name); AddEntityToCollection(entity); currentEntities.Add(entity); } } //Remove old entities from the project var entitiesToDelete = entitiesToRefresh.Except(currentEntities).ToList(); foreach (PMEntity entity in entitiesToDelete) { RemoveEntityFromCollection(entity); } }
/// <summary> /// Removes the PMEntity from its collection. It also removes from PowerMill. /// </summary> /// <param name="itemToRemove">The PMEntity to remove.</param> internal void RemoveEntityFromCollection(PMEntity itemToRemove) { if (itemToRemove.Identifier == PMNCProgram.NC_PROGRAM_IDENTIFIER) { _NCPrograms.Remove((PMNCProgram)itemToRemove); } else if (itemToRemove.Identifier == PMBoundary.BOUNDARY_IDENTIFIER) { _boundaries.Remove((PMBoundary)itemToRemove); } else if (itemToRemove.Identifier == PMFeatureSet.FEATURESET_IDENTIFIER) { _featureSets.Remove((PMFeatureSet)itemToRemove); } else if (itemToRemove.Identifier == PMGroup.GROUP_IDENTIFIER) { _groups.Remove((PMGroup)itemToRemove); } else if (itemToRemove.Identifier == PMLevelOrSet.LEVEL_OR_SET_IDENTIFIER) { _levelsOrSets.Remove((PMLevelOrSet)itemToRemove); } else if (itemToRemove.Identifier == PMModel.MODEL_IDENTIFIER) { _models.Remove((PMModel)itemToRemove); } else if (itemToRemove.Identifier == PMPattern.PATTERN_IDENTIFIER) { _patterns.Remove((PMPattern)itemToRemove); } else if (itemToRemove.Identifier == PMStockModel.STOCKMODEL_IDENTIFIER) { _stockModels.Remove((PMStockModel)itemToRemove); } else if (itemToRemove.Identifier == PMToolpath.TOOLPATH_IDENTIFIER) { _toolpaths.Remove((PMToolpath)itemToRemove); } else if (itemToRemove.Identifier == PMTool.TOOL_IDENTIFIER) { _tools.Remove((PMTool)itemToRemove); } else if (itemToRemove.Identifier == PMWorkplane.WORKPLANE_IDENTIFIER) { _workplanes.Remove((PMWorkplane)itemToRemove); } else if (itemToRemove.Identifier == PMMachineTool.MACHINE_TOOL_IDENTIFIER) { _machineTools.Remove((PMMachineTool)itemToRemove); } else { throw new ApplicationException("Identifier not supported"); } }
/// <summary> /// Duplicates an entity. It also updates PowerMill. /// </summary> public virtual PMEntity Duplicate() { PMEntity copiedItem = null; // Use Clipboard copy for a model to avoid empty model if (Identifier == "MODEL") { PowerMill.DoCommand("EDIT MODEL '" + _name + "' CLIPBOARD COPY"); PowerMill.DoCommand("CREATE MODEL CLIPBOARD"); } else { PowerMill.DoCommand("Copy " + Identifier + " '" + _name + "'"); } List <PMEntity> createdItems = PowerMill.ActiveProject.CreatedItems(); if (createdItems.Count == 1) { if (createdItems[0].Identifier == Identifier) { copiedItem = createdItems[0]; } else { throw new ApplicationException("Item not duplicated"); } } else { List <PMEntity> entLst = (from ent in createdItems where ent.Identifier == Identifier select ent).ToList(); if ((entLst.Count > 1) | (entLst.Count == 0)) { throw new ApplicationException("Too many items created. Do not know which is the one duplicated!"); } copiedItem = entLst[0]; //return item } if (copiedItem != null) { PowerMill.ActiveProject.AddEntityToCollection(copiedItem); } return(copiedItem); }
/// <summary> /// Duplicates an entity. It also updates PowerMill. /// </summary> public virtual PMEntity Duplicate() { PMEntity copiedItem = null; PowerMill.DoCommand("Copy " + Identifier + " '" + _name + "'"); List <PMEntity> createdItems = PowerMill.ActiveProject.CreatedItems(); if (createdItems.Count == 1) { if (createdItems[0].Identifier == Identifier) { copiedItem = createdItems[0]; } else { throw new ApplicationException("Item not duplicated"); } } else { List <PMEntity> entLst = (from ent in createdItems where ent.Identifier == Identifier select ent).ToList(); if ((entLst.Count > 1) | (entLst.Count == 0)) { throw new ApplicationException("Too many items created. Do not know which is the one duplicated!"); } copiedItem = entLst[0]; //return item } if (copiedItem != null) { PowerMill.ActiveProject.AddEntityToCollection(copiedItem); } return(copiedItem); }
/// <summary> /// Adds the PMEntity to its collection type. /// </summary> /// <param name="addItem">The PMEntity to add.</param> internal void AddEntityToCollection(PMEntity addItem) { if (addItem.Identifier == PMNCProgram.NC_PROGRAM_IDENTIFIER) { //find position in list and add it List <string> newList = _NCPrograms.ReadNCPrograms(); int idx = 0; int ipos = -1; for (idx = 0; idx <= newList.Count - 1; idx++) { if (newList[idx] == addItem.Name) { ipos = idx; break; // TODO: might not be correct. Was : Exit For } } if (ipos > -1) { if (ipos >= _NCPrograms.Count) { _NCPrograms.Add((PMNCProgram)addItem); } else { _NCPrograms.Insert(ipos, (PMNCProgram)addItem); } } else { throw new ApplicationException("item not found in NC Program List"); } } else if (addItem.Identifier == PMBoundary.BOUNDARY_IDENTIFIER) { _boundaries.Add((PMBoundary)addItem); } else if (addItem.Identifier == PMFeatureSet.FEATURESET_IDENTIFIER) { _featureSets.Add((PMFeatureSet)addItem); } else if (addItem.Identifier == PMGroup.GROUP_IDENTIFIER) { _groups.Add((PMGroup)addItem); } else if (addItem.Identifier == PMLevelOrSet.LEVEL_OR_SET_IDENTIFIER) { _levelsOrSets.Add((PMLevelOrSet)addItem); } else if (addItem.Identifier == PMModel.MODEL_IDENTIFIER) { _models.Add((PMModel)addItem); } else if (addItem.Identifier == PMPattern.PATTERN_IDENTIFIER) { _patterns.Add((PMPattern)addItem); } else if (addItem.Identifier == PMStockModel.STOCKMODEL_IDENTIFIER) { _stockModels.Add((PMStockModel)addItem); } else if (addItem.Identifier == PMToolpath.TOOLPATH_IDENTIFIER) { _toolpaths.Add((PMToolpath)addItem); } else if (addItem.Identifier == PMTool.TOOL_IDENTIFIER) { _tools.Add((PMTool)addItem); } else if (addItem.Identifier == PMWorkplane.WORKPLANE_IDENTIFIER) { _workplanes.Add((PMWorkplane)addItem); } else if (addItem.Identifier == PMMachineTool.MACHINE_TOOL_IDENTIFIER) { _machineTools.Add((PMMachineTool)addItem); } else { throw new ApplicationException("Identifier not supported"); } }