Пример #1
0
        /// <summary>
        /// 这里提供获取当前装配体的 bounding box
        /// </summary>
        /// <returns>返回 LxWxH</returns>
        private string GetActionAssemlbyBOX()
        {
            BoxSize newboxSize = new BoxSize();

            ModelDoc2 swModel = default(ModelDoc2);

            double L = 0;
            double W = 0;
            double H = 0;

            double[] BoxFeatureDblArray = new double[7];

            double[] BoxFaceDblArray = new double[7];

            SketchPoint[]   swSketchPt  = new SketchPoint[9];
            SketchSegment[] swSketchSeg = new SketchSegment[13];

            swModel = (ModelDoc2)iswApp.IActiveDoc2;

            AssemblyDoc assemblyDoc = (AssemblyDoc)swModel;

            BoxFeatureDblArray = (double[])assemblyDoc.GetBox((int)swBoundingBoxOptions_e.swBoundingBoxIncludeRefPlanes);

            L = (BoxFeatureDblArray[3] - BoxFeatureDblArray[0]) * 1000;
            W = (BoxFeatureDblArray[4] - BoxFeatureDblArray[1]) * 1000;
            H = (BoxFeatureDblArray[5] - BoxFeatureDblArray[2]) * 1000;

            List <double> myList = new List <double> {
                L, W, H
            };

            myList.Sort();

            newboxSize.Length = myList[2];
            newboxSize.Weigth = myList[1];
            newboxSize.Height = myList[0];

            bool b = swModel.Extension.SelectByID2("Size", "SKETCH", 0, 0, 0, false, 0, null, 0);

            if (b == false)
            {
                swModel.Insert3DSketch2(true);
                swModel.SetAddToDB(true);
                swModel.SetDisplayWhenAdded(false);

                SketchManager swSketchMgr = default(SketchManager);

                swSketchMgr = (SketchManager)swModel.SketchManager;
                // Draw points at each corner of bounding box
                swSketchPt[0] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[1], BoxFeatureDblArray[5]);
                swSketchPt[1] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[1], BoxFeatureDblArray[5]);
                swSketchPt[2] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[1], BoxFeatureDblArray[2]);
                swSketchPt[3] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[1], BoxFeatureDblArray[2]);
                swSketchPt[4] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[4], BoxFeatureDblArray[5]);
                swSketchPt[5] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[4], BoxFeatureDblArray[5]);
                swSketchPt[6] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[4], BoxFeatureDblArray[2]);
                swSketchPt[7] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[4], BoxFeatureDblArray[2]);
                // Now draw bounding box
                swSketchSeg[0]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[0].X, swSketchPt[0].Y, swSketchPt[0].Z, swSketchPt[1].X, swSketchPt[1].Y, swSketchPt[1].Z);
                swSketchSeg[1]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[1].X, swSketchPt[1].Y, swSketchPt[1].Z, swSketchPt[2].X, swSketchPt[2].Y, swSketchPt[2].Z);
                swSketchSeg[2]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[2].X, swSketchPt[2].Y, swSketchPt[2].Z, swSketchPt[3].X, swSketchPt[3].Y, swSketchPt[3].Z);
                swSketchSeg[3]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[3].X, swSketchPt[3].Y, swSketchPt[3].Z, swSketchPt[0].X, swSketchPt[0].Y, swSketchPt[0].Z);
                swSketchSeg[4]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[0].X, swSketchPt[0].Y, swSketchPt[0].Z, swSketchPt[4].X, swSketchPt[4].Y, swSketchPt[4].Z);
                swSketchSeg[5]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[1].X, swSketchPt[1].Y, swSketchPt[1].Z, swSketchPt[5].X, swSketchPt[5].Y, swSketchPt[5].Z);
                swSketchSeg[6]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[2].X, swSketchPt[2].Y, swSketchPt[2].Z, swSketchPt[6].X, swSketchPt[6].Y, swSketchPt[6].Z);
                swSketchSeg[7]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[3].X, swSketchPt[3].Y, swSketchPt[3].Z, swSketchPt[7].X, swSketchPt[7].Y, swSketchPt[7].Z);
                swSketchSeg[8]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[4].X, swSketchPt[4].Y, swSketchPt[4].Z, swSketchPt[5].X, swSketchPt[5].Y, swSketchPt[5].Z);
                swSketchSeg[9]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[5].X, swSketchPt[5].Y, swSketchPt[5].Z, swSketchPt[6].X, swSketchPt[6].Y, swSketchPt[6].Z);
                swSketchSeg[10] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[6].X, swSketchPt[6].Y, swSketchPt[6].Z, swSketchPt[7].X, swSketchPt[7].Y, swSketchPt[7].Z);
                swSketchSeg[11] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[7].X, swSketchPt[7].Y, swSketchPt[7].Z, swSketchPt[4].X, swSketchPt[4].Y, swSketchPt[4].Z);

                swModel.SetDisplayWhenAdded(true);
                swModel.SetAddToDB(false);

                //string actionSketchname = swModel.SketchManager.ActiveSketch.Name;

                swModel.Insert3DSketch2(true);

                swModel.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, true, false, "Size");
                swModel.ClearSelection2(true);

                swModel.Extension.SelectByID2("Size", "SKETCH", 0, 0, 0, false, 0, null, 0);

                swModel.BlankSketch();

                swModel.ClearSelection2(true);
            }

            BoxSize boxSize = new BoxSize();

            boxSize.Length = Math.Round(newboxSize.Length, 0);
            boxSize.Weigth = Math.Round(newboxSize.Weigth, 0);
            boxSize.Height = Math.Round(newboxSize.Height, 0);

            string proText = string.Join("x", boxSize.Length, boxSize.Weigth, boxSize.Height);

            return(proText);
        }
Пример #2
0
        public void GetBoundingBox(out BoxSize boxSize)
        {
            BoxSize newboxSize = new BoxSize();

            ModelDoc2 swModel = default(ModelDoc2);

            double L = 0;
            double W = 0;
            double H = 0;

            double[] BoxFeatureDblArray = new double[7];

            SketchManager swSketchMgr = default(SketchManager);

            SketchPoint[]   swSketchPt  = new SketchPoint[9];
            SketchSegment[] swSketchSeg = new SketchSegment[13];

            swModel = (ModelDoc2)iswApp.IActiveDoc2;

            PartDoc swPart = (PartDoc)swModel;

            double[] BoxFaceDblArray = new double[7];
            BoxFeatureDblArray = (double[])swPart.GetPartBox(true);

            Debug.Print("  Pt1 = " + "(" + BoxFeatureDblArray[0] * 1000.0 + ", " + BoxFeatureDblArray[1] * 1000.0 + ", " + BoxFeatureDblArray[2] * 1000.0 + ") mm");
            Debug.Print("  Pt2 = " + "(" + BoxFeatureDblArray[3] * 1000.0 + ", " + BoxFeatureDblArray[4] * 1000.0 + ", " + BoxFeatureDblArray[5] * 1000.0 + ") mm");
            L = (BoxFeatureDblArray[3] - BoxFeatureDblArray[0]) * 1000;
            W = (BoxFeatureDblArray[4] - BoxFeatureDblArray[1]) * 1000;
            H = (BoxFeatureDblArray[5] - BoxFeatureDblArray[2]) * 1000;

            List <double> myList = new List <double> {
                L, W, H
            };

            myList.Sort();

            newboxSize.Length = myList[2];
            newboxSize.Weigth = myList[1];
            newboxSize.Height = myList[0];

            swModel.Insert3DSketch2(true);
            swModel.SetAddToDB(true);
            swModel.SetDisplayWhenAdded(false);

            swSketchMgr = (SketchManager)swModel.SketchManager;
            // Draw points at each corner of bounding box
            swSketchPt[0] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[1], BoxFeatureDblArray[5]);
            swSketchPt[1] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[1], BoxFeatureDblArray[5]);
            swSketchPt[2] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[1], BoxFeatureDblArray[2]);
            swSketchPt[3] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[1], BoxFeatureDblArray[2]);
            swSketchPt[4] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[4], BoxFeatureDblArray[5]);
            swSketchPt[5] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[4], BoxFeatureDblArray[5]);
            swSketchPt[6] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[0], BoxFeatureDblArray[4], BoxFeatureDblArray[2]);
            swSketchPt[7] = (SketchPoint)swSketchMgr.CreatePoint(BoxFeatureDblArray[3], BoxFeatureDblArray[4], BoxFeatureDblArray[2]);
            // Now draw bounding box
            swSketchSeg[0]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[0].X, swSketchPt[0].Y, swSketchPt[0].Z, swSketchPt[1].X, swSketchPt[1].Y, swSketchPt[1].Z);
            swSketchSeg[1]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[1].X, swSketchPt[1].Y, swSketchPt[1].Z, swSketchPt[2].X, swSketchPt[2].Y, swSketchPt[2].Z);
            swSketchSeg[2]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[2].X, swSketchPt[2].Y, swSketchPt[2].Z, swSketchPt[3].X, swSketchPt[3].Y, swSketchPt[3].Z);
            swSketchSeg[3]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[3].X, swSketchPt[3].Y, swSketchPt[3].Z, swSketchPt[0].X, swSketchPt[0].Y, swSketchPt[0].Z);
            swSketchSeg[4]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[0].X, swSketchPt[0].Y, swSketchPt[0].Z, swSketchPt[4].X, swSketchPt[4].Y, swSketchPt[4].Z);
            swSketchSeg[5]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[1].X, swSketchPt[1].Y, swSketchPt[1].Z, swSketchPt[5].X, swSketchPt[5].Y, swSketchPt[5].Z);
            swSketchSeg[6]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[2].X, swSketchPt[2].Y, swSketchPt[2].Z, swSketchPt[6].X, swSketchPt[6].Y, swSketchPt[6].Z);
            swSketchSeg[7]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[3].X, swSketchPt[3].Y, swSketchPt[3].Z, swSketchPt[7].X, swSketchPt[7].Y, swSketchPt[7].Z);
            swSketchSeg[8]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[4].X, swSketchPt[4].Y, swSketchPt[4].Z, swSketchPt[5].X, swSketchPt[5].Y, swSketchPt[5].Z);
            swSketchSeg[9]  = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[5].X, swSketchPt[5].Y, swSketchPt[5].Z, swSketchPt[6].X, swSketchPt[6].Y, swSketchPt[6].Z);
            swSketchSeg[10] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[6].X, swSketchPt[6].Y, swSketchPt[6].Z, swSketchPt[7].X, swSketchPt[7].Y, swSketchPt[7].Z);
            swSketchSeg[11] = (SketchSegment)swSketchMgr.CreateLine(swSketchPt[7].X, swSketchPt[7].Y, swSketchPt[7].Z, swSketchPt[4].X, swSketchPt[4].Y, swSketchPt[4].Z);

            swModel.SetDisplayWhenAdded(true);
            swModel.SetAddToDB(false);

            //string actionSketchname = swModel.SketchManager.ActiveSketch.Name;

            swModel.Insert3DSketch2(true);

            swModel.SelectedFeatureProperties(0, 0, 0, 0, 0, 0, 0, true, false, "Size");

            swModel.ClearSelection2(true);

            boxSize.Length = newboxSize.Length;
            boxSize.Weigth = newboxSize.Weigth;
            boxSize.Height = newboxSize.Height;
            //  swModel.Extension.CustomPropertyManager[""].Delete("尺寸");

            swModel.Extension.CustomPropertyManager[""].Add3("尺寸", 30, "aaaa", 1);

            // swModel.AddCustomInfo("尺寸","Text", boxSize.Length.ToString());

            swModel.Save();
        }