4 Replies Latest reply on Sep 1, 2016 9:58 AM by William Cruz

    Edit / Modify a Reference Plane

    William Cruz

      What I'm trying to do:

      Edit a Reference Plane in a Component inside an Assembly.

           The Planes inside the component are broken and I'm trying to re-attach them to an external reference within the main Assembly file. See the image.

           I want to edit the "FINISH" refPlane in the component and make it's first reference the "FINISH" plane in the main assembly.

      Code:

                            
      /*EDIT COMP IN ASSEMBLY*/
                             int EditRefInfo = 0;
                             swModel.ClearSelection2(true);
                             boolstatus = swModel.Extension.SelectByID2(UpdatedCompNameSelectionString, "COMPONENT", 0, 0, 0, true, 0, null, 0);
                             swAssembly = ((AssemblyDoc)(swModel));
                             swAssembly.EditPart2(true, false, ref EditRefInfo);
                             boolstatus = swModel.ShowConfiguration2("Default");
      
      /*SELECT AND MODIFY*/
                             FeatureManager swFeatMgr = swModel.FeatureManager;
                             SelectionMgr swSelMrg = swModel.SelectionManager;
                             ModelDocExtension swModelDocExt = swModel.Extension;
                             Feature FeatureToAdjust = default(Feature);
                             Feature Replacement = default(Feature);
      
                             IRefPlaneFeatureData swRefPlaneData = default(IRefPlaneFeatureData);
                             IRefPlaneFeatureData NewSwRefPlaneData = default(IRefPlaneFeatureData);
      
                             boolstatus = swModel.Extension.SelectByID2("FINISH", "PLANE", 0, 0, 0, true, 0, null, 0);
                             Replacement = (Feature)swSelMrg.GetSelectedObject6(1, -1);
                             NewSwRefPlaneData = Replacement.GetDefinition();
                             boolstatus = NewSwRefPlaneData.AccessSelections(swModel, null);
      
                             swModel.ClearSelection2(true);
      
                             boolstatus = swModel.Extension.SelectByID2("FINISH@" + UpdatedCompNameSelectionString, "PLANE", 0, 0, 0, true, 0, null, 0);
                             FeatureToAdjust = (Feature)swSelMrg.GetSelectedObject6(1, -1);
                             swRefPlaneData = FeatureToAdjust.GetDefinition();
      
                             boolstatus = swRefPlaneData.AccessSelections(swModel, null);
      
                             swRefPlaneData.Reference[0] = Replacement;
      
                             FeatureToAdjust.ModifyDefinition(swRefPlaneData, swModel, null);
      
      
        • Re: Edit / Modify a Reference Plane
          Artem Taturevych

          You do not need those lines:

           

          NewSwRefPlaneData = Replacement.GetDefinition();

          boolstatus = NewSwRefPlaneData.AccessSelections(swModel, null);

          and try to change this line

          swRefPlaneData.Reference[0] = Replacement;

          to

          var curRefs = swRefPlaneData.Reference as object[];

          curRefs [0] = new System.Runtime.InteropServices.DispatchWrapper(Replacement);

          swRefPlaneData.Reference = curRefs;

          • Re: Edit / Modify a Reference Plane
            William Cruz

            The initial code labeled /*EDIT COMP IN ASSEMBLY*/ was not needed in my case. The rest of the code was modified and confirmed working.

             

                                     /*SELECT AND MODIFY*/
                                    //IFeatureManager swFeatMgr = swModel.FeatureManager;
                                    ISelectionMgr swSelMrg = swModel.SelectionManager;
                                    IModelDocExtension swModelDocExt = swModel.Extension;
                                    IFeature FeatureToAdjust = default(IFeature);
                                    IFeature Replacement = default(IFeature);
                                    IRefPlaneFeatureData swRefPlaneData = default(IRefPlaneFeatureData);
            
                                    /*ASSIGN EXTERNAL REF TO VAR*/
                                    boolstatus = swModelDocExt.SelectByID2("FINISH", "PLANE", 0, 0, 0, false, 0, null, 0);
                                    Replacement = swSelMrg.GetSelectedObject6(1, -1);
                                    swModel.ClearSelection2(true);
            
                                    /*GET REFPLANE TO BE CHANGED*/
                                    boolstatus = swModelDocExt.SelectByID2("FINISH@" + UpdatedCompNameSelectionString, "PLANE", 0, 0, 0, false, 0, null, 0);
                                    FeatureToAdjust = swSelMrg.GetSelectedObject6(1, -1);
            
                                    /*GET REFPLANE DATA*/
                                    swRefPlaneData = FeatureToAdjust.IGetDefinition();
                                    boolstatus = swRefPlaneData.IAccessSelections((ModelDoc2)swModel, ChildComp);
            
                                    /*VERIFY THAT THERE IS AT LEAST ONE SELECTION*/
                                    int count = swRefPlaneData.GetSelectionsCount();
                                    if (count == 1)
                                    {
                                        /*REATTACH EXTERNAL REFERENCES*/
                                        object selections = swRefPlaneData.IGetSelections(1);
                                        selections = Replacement;
                                        swRefPlaneData.ISetSelections(1, ref selections);
                                        swRefPlaneData.Distance = 0.0f;
            
                                        boolstatus = FeatureToAdjust.IModifyDefinition2(
                                            swRefPlaneData,
                                            (ModelDoc2)swModel,
                                            null);/*<--NULL FOR PART FILE - ICOMPONENT2 FOR ASSEMBLIES*/
                                    }
            
                                    /*CLEAN UP*/
                                    swModel.ClearSelection2(true);
                                    swAssembly = ((IAssemblyDoc)(swModel));
                                    swModel = ((IModelDoc2)(_swApp.ActiveDoc));
                                    swAssembly.EditAssembly();
            
              • Re: Edit / Modify a Reference Plane
                William Cruz

                The digested method for re-use, enjoy

                FYI: The PlaneName is the same in my case, modify as needed.

                 

                          internal void swReAttchExternalRef(ref IModelDoc2 swModel, ref Component2 ChildComp, string PlanName, string SelectionString)
                        {
                            try
                            {
                                bool boolstatus = false;
                                IAssemblyDoc swAssembly = (IAssemblyDoc)swModel;
                
                                /*SELECT AND MODIFY*/
                                ISelectionMgr swSelMrg = swModel.SelectionManager;
                                IModelDocExtension swModelDocExt = swModel.Extension;
                                IFeature FeatureToAdjust = default(IFeature);
                                IFeature Replacement = default(IFeature);
                                IRefPlaneFeatureData swRefPlaneData = default(IRefPlaneFeatureData);
                
                                /*ASSIGN EXTERNAL REF TO VAR*/
                                boolstatus = swModelDocExt.SelectByID2(PlanName, "PLANE", 0, 0, 0, false, 0, null, 0);
                                Replacement = swSelMrg.GetSelectedObject6(1, -1);
                                swModel.ClearSelection2(true);
                
                                /*GET REFPLANE TO BE CHANGED*/
                                boolstatus = swModelDocExt.SelectByID2(PlanName + "@" + SelectionString, "PLANE", 0, 0, 0, false, 0, null, 0);
                                FeatureToAdjust = swSelMrg.GetSelectedObject6(1, -1);
                
                                /*GET REFPLANE DATA*/
                                swRefPlaneData = FeatureToAdjust.IGetDefinition();
                                boolstatus = swRefPlaneData.IAccessSelections((ModelDoc2)swModel, ChildComp);
                
                                /*VERIFY THAT THERE IS AT LEAST ONE SELECTION*/
                                int count = swRefPlaneData.GetSelectionsCount();
                                if (count == 1)
                                {
                                    /*REATTACH EXTERNAL REFERENCES*/
                                    object selections = swRefPlaneData.IGetSelections(1);
                                    selections = Replacement;
                                    swRefPlaneData.ISetSelections(1, ref selections);
                                    swRefPlaneData.Distance = 0.0f;
                
                                    boolstatus = FeatureToAdjust.IModifyDefinition2(
                                        swRefPlaneData,
                                        (ModelDoc2)swModel,
                                        null);/*<--NULL FOR PART FILE - ICOMPONENT2 FOR ASSEMBLIES*/
                                }
                
                                /*CLEAN UP*/
                                swModel.ClearSelection2(true);
                                swAssembly = ((IAssemblyDoc)(swModel));
                                swModel = ((IModelDoc2)(_swApp.ActiveDoc));
                                swModel.EditRebuild3();
                            }
                            catch (Exception ex)
                            {
                                LogIt.LogError(ex);
                            }
                        }