C# SolidWorks secondary development API---delete all features of parts (retain entities)

Sometimes we don't want to send the internal details of the parts to the customer, usually we will compress some internal features. Then transfer it out, but some associations in the compression and decompression process are more difficult to control.
As shown in the figure below, there is a small hole in the middle:
Original parts
If you don’t want to turn out this hole, you can use the Defeature under Tools to do it:
Insert picture description here
This way we can get a new part, so that the internal features are well protected:
Insert picture description here
but this step does not seem to find anything api.

In some cases, our parts are relatively simple and there are no secrets. Of course, you can directly use the third format to export, so that you can directly become an entity without steps.
Today we will use some api functions to directly delete features without exporting them, but keep entities. I think it is still useful in some cases.
The general idea is as follows:

  1. Traverse all entities and save them inside the program
  2. Delete all features in the current part
  3. Generate independent entity features from internal entities

Insert picture description here

Here is the code:

 /// <summary>
        /// 删除零件特征,但保留实体,类似于导出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeleteFeature_Click(object sender, EventArgs e)
        {
    
    
            ISldWorks swApp = Utility.ConnectToSolidWorks();

            var swModel = (ModelDoc2)swApp.ActiveDoc;

            if (swModel != null)
            {
    
    
                PartDoc part = (PartDoc)swModel;
                var vBodies = GetBodyCopies(part);

                DeleteAllUserFeature(swModel);

                CreateFeatureForBodies(part, vBodies);
            }
        }

        /// <summary>
        /// 获取零件实体的备份
        /// </summary>
        /// <param name="partDoc"></param>
        /// <returns></returns>
        private Body2[] GetBodyCopies(PartDoc partDoc)
        {
    
    
            var vBodies = partDoc.GetBodies2((int)swBodyType_e.swAllBodies, true);

            Body2[] newBodies = new Body2[vBodies.Length];

            for (int i = 0; i < vBodies.Length; i++)
            {
    
    
                var swBody2 = (Body2)vBodies[i];
                newBodies[i] = swBody2.Copy();
            }

            return newBodies;
        }

        /// <summary>
        /// 把备份的实体 生成特征
        /// </summary>
        /// <param name="partDoc"></param>
        /// <param name="bodies"></param>
        private void CreateFeatureForBodies(PartDoc partDoc, Body2[] bodies)
        {
    
    
            for (int i = 0; i < bodies.Length; i++)
            {
    
    
                partDoc.CreateFeatureFromBody3(bodies[i], false, (int)swCreateFeatureBodyOpts_e.swCreateFeatureBodySimplify);
            }
        }

        /// <summary>
        /// 删除当前所有的特征
        /// </summary>
        /// <param name="modelDoc2"></param>
        private void DeleteAllUserFeature(ModelDoc2 modelDoc2)
        {
    
    
            SelectAllUserFeature(modelDoc2);
            modelDoc2.Extension.DeleteSelection2((int)swDeleteSelectionOptions_e.swDelete_Children + (int)swDeleteSelectionOptions_e.swDelete_Absorbed);
        }

        /// <summary>
        /// 选择所有的特征
        /// </summary>
        /// <param name="modelDoc2"></param>
        private void SelectAllUserFeature(ModelDoc2 modelDoc2)
        {
    
    
            modelDoc2.ClearSelection2(true);

            var swFeature = (Feature)modelDoc2.FirstFeature();

            // var selectFeat = false;

            while (swFeature != null)
            {
    
    
                if (swFeature != null)
                {
    
    
                    swFeature.Select2(true, 1);
                }
                else
                {
    
    
                    if (swFeature.GetTypeName2() == "OriginProfileFeature")
                    {
    
    
                        //  selectFeat = true;
                    }
                }

                swFeature = swFeature.GetNextFeature();
            }
        }

After running, the result is as follows: Just like after export, export again, it still has the same name. It's just that the features are gone.
Insert picture description here

The source code has been uploaded.

Guess you like

Origin blog.csdn.net/zengqh0314/article/details/106272356