8 Replies Latest reply on Feb 18, 2016 5:50 AM by Johannes Kronberger

    Is it possible to take advantage of built in features within the regenerate method of a macro feature?

    Johannes Kronberger

      Hi all,

       

      I'm building a macro feature. I would like to perform a FLEX operation, wrapping a solid body around a cylinder. I can do this easily in Solidworks with the flex tool and I can write a macro to do this. However is this possible to do within the rebuild method of a macro feature.

       

      It says here.

       

      2011 SOLIDWORKS API Help - Rebuild Function 

      No new features can be introduced and no user interaction can occur during a rebuild of a model containing a macro feature.

       

      Now I don't actually want to create a flex feature. I just want to use the IBody2 result that comes out of it and return that as the result of the rebuild function.

        • Re: Is it possible to take advantage of built in features within the regenerate method of a macro feature?
          Artem Taturevych

          Hi,

           

          if you want to do this - you better do it in a new temp file otherwise it will be a lot of stability problems if you try to do this within regenerate method.

           

          As an alternative I would suggest the following workaround

           

          • When you insert your macro feature you firstly create a wrap feature
          • Then add this wrap feature as the parent of your macro feature
          • In this case you do not need to create any new features within the rebuild rather get the body of its parent (which is a wrap feature) and modify it as needed.

           

          So your macro feature become an extension on top of the wrap feature which can modify its bodies.

           

          Thanks,
          Artem

            • Re: Is it possible to take advantage of built in features within the regenerate method of a macro feature?
              Johannes Kronberger

              Hi Artem,

               

              The process we want is.

               

              (1) Macro feature asks user to select two sketches

              (2) We take those two sketches and using IModeller we generate a solid

              (3) We take solid from (2) and wrap it around a cylinder using flex

              (5) Merge the result of (3) with another solid body generated from IModeller

              (4) Take wrapped solid from step (5) and return it as the result of the rebuild function

               

              So the flex/wrap operation is just an intermediate operation. In this case I suspect using a temp file will be the best. Are you able to point me at the API's or any examples that I can refer to to build and extract a body from a temp file?

               

              Thanks

                • Re: Is it possible to take advantage of built in features within the regenerate method of a macro feature?
                  Artem Taturevych

                  Hi Johannes,

                   

                  I would recommend the following scenario

                  (1) Macro feature asks user to select two sketches

                  (2) We take those two sketches and using IModeller we generate a solid

                  Create a macro feature here which just generates the solid and returns it from regenerate method (feature A)

                  (3) We take solid from (2) and wrap it around a cylinder using flex

                  Get the solid body (IFeature::GetFaces=>IFace2::GetBody) and create Wrap feature using SolidWorks feature manager (feature B). in this case Feature B is the parent of feature A and will be automatically regenerated once feature A is changed (for example sketch segments changed)

                  (5) Merge the result of (3) with another solid body generated from IModeller

                  Again get the solid body from the feature B and put it as edit body to another macro feature (feature C)

                  (4) Take wrapped solid from step (5) and return it as the result of the rebuild function

                  Do your merging and return new body

                   

                  So you have 3 features instead of one but because of correct parent-child relationship you will have correct behavior.

                   

                  Regarding the temp document. What I mean is generate new document every time rebuild is happening (its may be hidden so user doesn't see it ISldWorks::DocumentVisible). Paste the body in there (IBody2::Copy->PartDoc::CreateFeatureFromBody3) add flex, get body and create temp body from it. Then close the temp file.

                   

                  Thanks,

                  Atrem

                    • Re: Is it possible to take advantage of built in features within the regenerate method of a macro feature?
                      Johannes Kronberger

                      Hi Artem,

                       

                      We have tried the following and it almost works.

                       

                              IBody2 Offset(IBody2 body)

                              {

                                  body = (IBody2) body.Copy();

                                  var partTemplateName = SwApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart);

                                  var model = (ModelDoc2)SwApp.NewDocument(partTemplateName, (int)swDwgPaperSizes_e.swDwgPaperAsize, 0.0, 0.0);

                                  var selMgr = (ISelectionMgr)model.SelectionManager;

                                  var featMgr = model.FeatureManager;

                                  var part = (PartDoc)model;

                                  var feat = (IFeature)part.CreateFeatureFromBody3(body, false, 0);

                                  model.Extension.SelectByID2(feat.Name, "BODYFEATURE", 0, 0, 0, false, -1, null, 0);

                                  if (selMgr.GetSelectedObjectCount2(-1) == 0)

                                  {

                                      MessageBox.Show("Unable to select objects");

                                      return body;

                                  }

                                  var feature = (IFeature)featMgr.InsertMoveFace(0, false, 0, 0.03);

                                  if (feature == null)

                                  {

                                      MessageBox.Show("Unable to move face");

                                      return null;

                                  }

                                  model.ForceRebuild3(false);

                                  var newBody = (IBody2) ((IBody2) part.Body()).Copy();

                                  SwApp.CloseDoc(model.GetTitle());

                                  return newBody;

                              }

                       

                      It all works up to InsertMoveFace which always returns null. Any idea why?

                       

                      We can see that the document is created and the original body is inserted and selected.