/// <summary>
        /// 编辑零部件,自动区分装配体还是零件
        /// </summary>
        /// <param name="comp">组件</param>
        /// <param name="doc">顶级文档</param>
        /// <param name="silent">静默编辑--提高速度</param>
        /// <param name="readOnly">只读</param>
        /// <returns></returns>
        public static swEditPartCommandStatus_e Edit(this IComponent2 comp, IAssemblyDoc doc, bool silent = true, bool readOnly = false)
        {
            var type = comp.GetCompType();
            int info = -1;

            switch (type)
            {
            case swDocumentTypes_e.swDocPART:
                if (comp.Select2(false, -1))
                {
                    doc.EditPart2(silent, readOnly, ref info);
                }
                break;

            case swDocumentTypes_e.swDocASSEMBLY:
                if (comp.Select2(false, -1))
                {
                    doc.EditAssembly();
                }
                info = 0;
                break;

            default:
                throw new FileFormatException(string.Format("can not edit component width type of:{0} ", type.ToString()));
            }
            swEditPartCommandStatus_e state = (swEditPartCommandStatus_e)info;

            return(state);
        }
Example #2
0
        private void SetExtrudeDepth(IAssemblyDoc assy, IComponent2 comp, string extrudeFeatName, double depth)
        {
            if (comp != null)
            {
                if (comp.GetSuppression() == (int)swComponentSuppressionState_e.swComponentLightweight ||
                    comp.GetSuppression() == (int)swComponentSuppressionState_e.swComponentFullyLightweight)
                {
                    if (comp.SetSuppression2((int)swComponentSuppressionState_e.swComponentResolved) != (int)swSuppressionError_e.swSuppressionChangeOk)
                    {
                        throw new InvalidOperationException("Failed to set component state to resolved");
                    }
                }
                else if (comp.GetSuppression() == (int)swComponentSuppressionState_e.swComponentSuppressed)
                {
                    throw new NotSupportedException("Suppressed component is not supported");
                }

                var feat = comp.FeatureByName(extrudeFeatName);

                if (feat == null)
                {
                    throw new MissingMemberException("Feature is not found in the component");
                }

                if (comp.Select4(false, null, false))
                {
                    int info = -1;
                    if (assy.EditPart2(true, false, ref info) == (int)swEditPartCommandStatus_e.swEditPartSuccessful)
                    {
                        var featData = feat.GetDefinition() as IExtrudeFeatureData2;

                        if (featData != null)
                        {
                            if (featData.AccessSelections(assy, comp))
                            {
                                featData.SetDepth(true, depth);
                                if (feat.ModifyDefinition(featData, assy, comp))
                                {
                                    (assy as IModelDoc2).ClearSelection2(true);
                                    assy.EditAssembly();//Exit edit part mode
                                }
                                else
                                {
                                    throw new Exception("Failed to set the depth to the feature");
                                }
                            }
                            else
                            {
                                throw new InvalidOperationException("Failed to access feature");
                            }
                        }
                        else
                        {
                            throw new InvalidCastException("Selected feature is not an extrude feature");
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException("Failed to edit part");
                    }
                }
                else
                {
                    throw new InvalidOperationException("Failed to select component");
                }
            }
            else
            {
                throw new Exception("Select component");
            }
        }