Пример #1
0
        /// <summary>
        /// 有无待核算体
        /// </summary>
        /// <param name="bodies">待核算体</param>
        /// <param name="swcostingpart">CostPart参数</param>
        /// <param name="type">零件类型</param>
        /// <returns>是否有</returns>
        private bool hasBody(int bodies, CostPart swcostingpart, HZ_EnumType type)
        {
            bool isBody = false;

            if ((bodies > 0))
            {
                var      costingBodies   = (object[])swcostingpart.GetBodies();
                CostBody swCostingBody   = (CostBody)costingBodies[0];
                string   costingBodyName = swCostingBody.GetName();
                // 确保是机加工零件
                if ((swCostingBody.GetBodyType() == (int)swcBodyType_e.swcBodyType_Machined))
                {
                    isBody = true;
                    switch ((int)swCostingBody.BodyStatus)
                    {
                    case (int)swcBodyStatus_e.swcBodyStatus_Analysed:
                        // 得到模板
                        m_swCostingAnalysis = swCostingBody.CreateCostAnalysis((selectCostingTemp(type)));
                        m_swCostingAnalysis = swCostingBody.GetCostAnalysis();
                        break;

                    default:
                        isBody = false;
                        break;
                    }
                }
            }
            return(isBody);
        }
Пример #2
0
        private void button3_Click(object sender, EventArgs e)
        {
            HZ_EnumType type = HZ_EnumType.hz_SimpleTurning; //工艺

            int          lotSize       = 100;                //批量大小
            int          totalNum      = 100;                //零件总个数
            string       materialClass = "";                 //材料类型
            string       materialName  = "";                 //材料名称
            HZ_StockType stocktype     = HZ_StockType.Block; //配料类型


            //Solidworks程序对象
            m_SwApp = (SldWorks)Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application"));

            //打开文件
            m_ModelDoc = openModle(partfilepath);

            //生成缩略图
            // ModelView LoMyModelView; //生成缩略图
            //LoMyModelView = m_ModelDoc.ActiveView;
            //LoMyModelView.FrameState = (int)swWindowState_e.swWindowMaximized;
            ////m_ModelDoc.ShowNamedView2("*Isometric", 7);
            //m_ModelDoc.ViewZoomtofit2();
            //m_ModelDoc.SaveAs(Application.StartupPath + "\\test.jpg");
            //m_ModelDoc.SaveAs(Application.StartupPath + "\\test.stl");


            //仅支持零部件
            if (m_ModelDoc.GetType() != (int)swDocumentTypes_e.swDocPART)
            {
                MessageBox.Show("仅支持零部件!");
                return;
            }
            bool   isSheetMetal = false;
            bool   isSolidBody  = false;
            bool   isBlank      = false;
            string strError     = string.Empty;

            //仅支持单实体零部件
            if (isMutiBodyOrSheetMetal(ref isBlank, ref isSheetMetal, ref isSolidBody, ref strError))
            {
                MessageBox.Show("仅支持单体零部件!");
                return;
            }

            //空白图纸
            if (isBlank)
            {
                MessageBox.Show("空白图纸!");
                return;
            }

            //不支持钣金件
            if (isSheetMetal)
            {
                MessageBox.Show("不支持钣金件!");
                return;
            }
            //是否为实体零件
            if (!isSolidBody)
            {
                MessageBox.Show("非实体零部件!");
                return;
            }

            //获取SolidWorks扩展器
            ModelDocExtension swModelDocExt = m_ModelDoc.Extension;

            // 初始化成本输出对象
            HZ_CostingOutput CostingOutput = new HZ_CostingOutput();

            //详细输出对象
            HZ_MassProperty massOutput = new HZ_MassProperty();

            // 得到质量属性 Part Mass
            getMass("", ref massOutput);

            // 得到外形尺寸属性
            getBox(ref massOutput);

            swCosting = (CostManager)swModelDocExt.GetCostingManager();
            swCosting.WaitForUIUpdate();

            // 得到 Costing part
            object swCostingModel = (object)swCosting.CostingModel;

            swCostingPart = (CostPart)swCostingModel;

            //设置默认模块参数
            //模板,批量大小,零件总个数,材料类型,材料名称,计算方式,配料类型
            swcCostingDefaults = (CostingDefaults)swCosting.CostingDefaults;
            swcCostingDefaults.SetTemplateName((int)swcCostingType_e.swcCostingType_Machining, selectCostingTemp(type));

            //单体零件
            swcCostingDefaults.LotSizeForSingleBody            = lotSize;
            swcCostingDefaults.TotalNumberOfPartsForSingleBody = totalNum;

            //多实体文件
            swcCostingDefaults.LotSizeForMultibody            = lotSize;
            swcCostingDefaults.TotalNumberOfPartsForMultibody = totalNum;

            //设置材料以及材料处理方式
            swcCostingDefaults.SetMaterialClass((int)swcMethodType_e.swcMethodType_Machining, materialClass);
            swcCostingDefaults.SetMaterialName((int)swcMethodType_e.swcMethodType_Machining, materialName);
            swcCostingDefaults.SetManufacturingMethod((int)swcBodyType_e.swcBodyType_Machined, (int)swcBodyType_e.swcBodyType_Machined);
            swcCostingDefaults.MachiningStockBodyType = (int)stocktype;

            //获取Cost Body
            swCostingBody = swCostingPart.SetCostingMethod("", (int)swcMethodType_e.swcMethodType_Machining);
            if (swCostingBody == null)
            {
                MessageBox.Show("制造成本计算失败");
                return;
            }


            // 创建 common Costing analysis
            m_swCostingAnalysis = swCostingBody.CreateCostAnalysis(selectCostingTemp(type));
            m_swCostingAnalysis = swCostingBody.GetCostAnalysis();
            m_swCostingAnalysis.TotalQuantity = totalNum;
            m_swCostingAnalysis.LotSize       = lotSize;

            CostFeature swCostingFeat        = default(CostFeature);
            CostFeature swCostingNextFeat    = default(CostFeature);
            CostFeature swCostingSubFeat     = default(CostFeature);
            CostFeature swCostingNextSubFeat = default(CostFeature);

            swCostingFeat = (CostFeature)m_swCostingAnalysis.GetFirstCostFeature();
            while ((swCostingFeat != null))
            {
                //  swcCostFeatureType_e.

                Debug.Print("    Feature: " + swCostingFeat.Name);
                Debug.Print("      Type: " + swCostingFeat.GetType());
                Debug.Print("        Setup related: " + swCostingFeat.IsSetup);
                Debug.Print("        Overridden: " + swCostingFeat.IsOverridden);
                Debug.Print("        Combined cost: " + swCostingFeat.CombinedCost);
                Debug.Print("        Combined time: " + swCostingFeat.CombinedTime);

                swCostingSubFeat = swCostingFeat.GetFirstSubFeature();
                while ((swCostingSubFeat != null))
                {
                    Debug.Print("      Subfeature: " + swCostingSubFeat.Name);
                    Debug.Print("        Type: " + swCostingSubFeat.GetType());
                    Debug.Print("          Setup related: " + swCostingSubFeat.IsSetup);
                    Debug.Print("          Overridden: " + swCostingSubFeat.IsOverridden);
                    Debug.Print("          Combined cost: " + swCostingSubFeat.CombinedCost);
                    Debug.Print("          Combined time: " + swCostingSubFeat.CombinedTime);

                    swCostingNextSubFeat = (CostFeature)swCostingSubFeat.GetNextFeature();
                    swCostingSubFeat     = null;
                    swCostingSubFeat     = (CostFeature)swCostingNextSubFeat;
                    swCostingNextSubFeat = null;
                }
                swCostingNextFeat = swCostingFeat.GetNextFeature();
                swCostingFeat     = null;
                swCostingFeat     = (CostFeature)swCostingNextFeat;
                swCostingNextFeat = null;
            }
        }
Пример #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            DateTime dt1 = DateTime.Now;

            HZ_EnumType type = HZ_EnumType.hz_SimpleTurning; //工艺

            int          lotSize       = 100;                //批量大小
            int          totalNum      = 100;                //零件总个数
            string       materialClass = "";                 //材料类型
            string       materialName  = "";                 //材料名称
            HZ_StockType stocktype     = HZ_StockType.Block; //配料类型


            //Solidworks程序对象
            m_SwApp = (SldWorks)Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application"));

            //打开文件
            m_ModelDoc = openModle(partfilepath);

            //生成缩略图
            // ModelView LoMyModelView; //生成缩略图
            //LoMyModelView = m_ModelDoc.ActiveView;
            //LoMyModelView.FrameState = (int)swWindowState_e.swWindowMaximized;
            ////m_ModelDoc.ShowNamedView2("*Isometric", 7);
            //m_ModelDoc.ViewZoomtofit2();
            //m_ModelDoc.SaveAs(Application.StartupPath + "\\test.jpg");
            //m_ModelDoc.SaveAs(Application.StartupPath + "\\test.stl");


            //仅支持零部件
            if (m_ModelDoc.GetType() != (int)swDocumentTypes_e.swDocPART)
            {
                MessageBox.Show("仅支持零部件!");
                return;
            }
            bool   isSheetMetal = false;
            bool   isSolidBody  = false;
            bool   isBlank      = false;
            string strError     = string.Empty;

            //仅支持单实体零部件
            if (isMutiBodyOrSheetMetal(ref isBlank, ref isSheetMetal, ref isSolidBody, ref strError))
            {
                MessageBox.Show("仅支持单体零部件!");
                return;
            }

            //空白图纸
            if (isBlank)
            {
                MessageBox.Show("空白图纸!");
                return;
            }

            //不支持钣金件
            if (isSheetMetal)
            {
                MessageBox.Show("不支持钣金件!");
                return;
            }
            //是否为实体零件
            if (!isSolidBody)
            {
                MessageBox.Show("非实体零部件!");
                return;
            }

            //获取SolidWorks扩展器
            ModelDocExtension swModelDocExt = m_ModelDoc.Extension;

            // 初始化成本输出对象
            HZ_CostingOutput CostingOutput = new HZ_CostingOutput();

            //详细输出对象
            HZ_MassProperty massOutput = new HZ_MassProperty();

            // 得到质量属性 Part Mass
            getMass("", ref massOutput);

            // 得到外形尺寸属性
            getBox(ref massOutput);

            swCosting = (CostManager)swModelDocExt.GetCostingManager();
            swCosting.WaitForUIUpdate();

            // 得到 Costing part
            object swCostingModel = (object)swCosting.CostingModel;

            swCostingPart = (CostPart)swCostingModel;

            //设置默认模块参数
            //模板,批量大小,零件总个数,材料类型,材料名称,计算方式,配料类型
            swcCostingDefaults = (CostingDefaults)swCosting.CostingDefaults;
            swcCostingDefaults.SetTemplateName((int)swcCostingType_e.swcCostingType_Machining, selectCostingTemp(type));

            //单体零件
            swcCostingDefaults.LotSizeForSingleBody            = lotSize;
            swcCostingDefaults.TotalNumberOfPartsForSingleBody = totalNum;

            //多实体文件
            swcCostingDefaults.LotSizeForMultibody            = lotSize;
            swcCostingDefaults.TotalNumberOfPartsForMultibody = totalNum;

            //设置材料以及材料处理方式
            swcCostingDefaults.SetMaterialClass((int)swcMethodType_e.swcMethodType_Machining, materialClass);
            swcCostingDefaults.SetMaterialName((int)swcMethodType_e.swcMethodType_Machining, materialName);
            swcCostingDefaults.SetManufacturingMethod((int)swcBodyType_e.swcBodyType_Machined, (int)swcBodyType_e.swcBodyType_Machined);
            swcCostingDefaults.MachiningStockBodyType = (int)stocktype;

            //获取Cost Body
            swCostingBody = swCostingPart.SetCostingMethod("", (int)swcMethodType_e.swcMethodType_Machining);
            if (swCostingBody == null)
            {
                MessageBox.Show("制造成本计算失败");
                return;
            }


            // 创建 common Costing analysis
            m_swCostingAnalysis = swCostingBody.CreateCostAnalysis(selectCostingTemp(type));
            m_swCostingAnalysis = swCostingBody.GetCostAnalysis();
            m_swCostingAnalysis.TotalQuantity = totalNum;
            m_swCostingAnalysis.LotSize       = lotSize;


            // 得到 Costing bodies
            int nbrCostingBodies = swCostingPart.GetBodyCount();

            if (hasBody(nbrCostingBodies, swCostingPart, type))
            {
                // 得到 machining Costing Analysis 数据
                getMachiningCostingAnalysisData(CostingOutput, ref massOutput, stocktype, materialClass, materialName);

                //得到返回的质量属性
                CostingOutput.massProperty = massOutput;

                // 得到 common Costing analysis 数据
                getCommonCostingAnalysisData(CostingOutput, totalNum, lotSize);

                //孔超标数据,标准 > 5 直接剔除,并+2 元,处理倒角导致成本超高的问题
                getCostingFeatures(ref CostingOutput);
            }

            DateTime dt2 = DateTime.Now;

            TimeSpan dt3 = dt2 - dt1;

            int goTime = dt3.Seconds;

            MessageBox.Show(goTime.ToString() + "秒");
        }