예제 #1
0
 public SeplaLockingVector()
 {
     _lockingVector     = new LockingVector();
     _manualLockStateId = _lockingVector.PauseManualLock();
 }
예제 #2
0
        public static MDPart CreateOrUpdateWithFunctionTemplate(ArticleReference articleReference)
        {
            // Need to lock project
            var project = articleReference.Project;

            project.SmartLock();
            if (articleReference.ParentObject != null)
            {
                articleReference.ParentObject.SmartLock();
            }
            articleReference.SmartLock();

            // Init
            var partsDatabase = new MDPartsManagement().OpenDatabase();

            //var articleReference = function.ArticleReferences.First();
            articleReference.SmartLock();
            var    partNr      = articleReference.PartNr;
            var    partVariant = articleReference.VariantNr;
            MDPart part        = partsDatabase.GetPart(partNr, partVariant);

            // Check if article is in project and remove, because the eplan action to create is not possible
            var existingArticle = project.Articles
                                  .FirstOrDefault(obj =>
                                                  obj.PartNr.Equals(partNr) && obj.VariantNr.Equals(partVariant)
                                                  );

            if (existingArticle != null)
            {
                existingArticle.SmartLock();
                existingArticle.Remove();
            }

            // Need to focus again if its lost
            new Edit().BringToFront((Placement)articleReference.ParentObject);

            // Create new part
            if (part == null)
            {
                // LockingVector is needed because of locking exception from EPLAN action (no catch possible)
                LockingVector lockingVector = new LockingVector();
                int           stateId       = lockingVector.PauseManualLock();
                new CommandLineInterpreter().Execute("XPameCreateType");
                lockingVector.ResumeManualLock(stateId);

                partsDatabase = new MDPartsManagement().OpenDatabase(); // Second Call needed to get new part
                part          = partsDatabase.GetPart(partNr, partVariant);
            }
            // Existing part
            else
            {
                // Rename part
                string suffix     = "_temp";
                string partNrTemp = part.PartNr + suffix;
                try
                {
                    articleReference.PartNr = partNrTemp;
                    articleReference.StoreToObject();

                    // Quiet create temp part
                    var application = new EplApplication();
                    var quiteMode   = application.QuietMode;
                    application.QuietMode = EplApplication.QuietModes.ShowNoDialogs;
                    new CommandLineInterpreter().Execute("XPameCreateType");
                    application.QuietMode = quiteMode;
                }
                finally
                {
                    // Rename back
                    articleReference.PartNr = partNr;
                    articleReference.StoreToObject();
                }

                // Copy FunctionTemplate
                partsDatabase = new MDPartsManagement().OpenDatabase(); // Second Call needed to get new part
                MDPart partDuplicate = partsDatabase.GetPart(partNrTemp, partVariant);
                foreach (var partFunctionTemplatePosition in part.FunctionTemplatePositions)
                {
                    part.RemoveFunctionTemplatePosition(partFunctionTemplatePosition);
                }
                foreach (var partDuplicateFunctionTemplatePosition in partDuplicate.FunctionTemplatePositions)
                {
                    part.AddFunctionTemplatePosition(partDuplicateFunctionTemplatePosition);
                }

                partsDatabase.RemovePart(partDuplicate);


                // Check if article is in project
                var existingTempArticle = project.Articles
                                          .FirstOrDefault(obj =>
                                                          obj.PartNr.Equals(partNrTemp) && obj.VariantNr.Equals(partVariant)
                                                          );
                if (existingTempArticle != null)
                {
                    existingTempArticle.SmartLock();
                    existingTempArticle.Remove();
                }
            }

            // Load data
            var article = project.Articles
                          .FirstOrDefault(obj =>
                                          obj.PartNr.Equals(partNr) && obj.VariantNr.Equals(partVariant)
                                          );

            if (article != null)
            {
                article.SmartLock();
                article.LoadFromMasterdata();
            }

            return(part);
        }