Ejemplo n.º 1
0
        internal static bool processModel(SldWorks swApp, string file, string targetFile, string calcFile, CancellationToken cancellationToken)
        {
            // Initiate variables
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
            StreamWriter toFile = new StreamWriter(targetFile);

            toFile.AutoFlush = true;
            StreamWriter toCalc = new StreamWriter(calcFile);

            toCalc.AutoFlush = true;
            MathUtility swMathUtil = default(MathUtility);
            ModelDoc2   swModel    = default(ModelDoc2);
            Feature     swFeat     = default(Feature);
            Feature     swMateFeat = null;
            Feature     swSubFeat  = default(Feature);
            Mate2       swMate     = default(Mate2);
            Component2  swComp     = default(Component2);

            MateEntity2[] swMateEnt  = new MateEntity2[3];
            MathTransform swTrans    = default(MathTransform);
            MathPoint     swOrig     = default(MathPoint);
            AssemblyDoc   swAssembly = default(AssemblyDoc);

            double[] corners   = new double[6];
            int[]    swAssyDir = new int[6];
            double[] nPt       = new double[3];
            object   vPt       = null;
            double   height    = 0;
            double   width     = 0;
            double   depth     = 0;
            int      Warning   = 0;
            int      Error     = 0;
            int      i         = 0;

            double[] entityParameters = new double[8];
            // Start function
            try
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(false);
                }
                string extention = Path.GetExtension(file);
                int    type      = 0;
                if (extention.ToLower().Contains("sldprt"))
                {
                    type = (int)swDocumentTypes_e.swDocPART;
                }
                else
                {
                    type = (int)swDocumentTypes_e.swDocASSEMBLY;
                }
                // Get assembly model
                swModel = swApp.OpenDoc6(file, type, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref Error, ref Warning) as ModelDoc2;
                if (Error != 0)
                {
                    return(false);
                }
                if (swModel == null)
                {
                    return(false);
                }
                swModel.Visible = true;
                // Get first assembly feature
                swFeat = (Feature)swModel.FirstFeature();
                // Iterate over features in FeatureManager design tree
                while ((swFeat != null))
                {
                    if ("MateGroup" == swFeat.GetTypeName())
                    {
                        swMateFeat = swFeat;
                        break;
                    }
                    swFeat = swFeat = swFeat.GetNextFeature();
                }
                toFile.WriteLine(" " + swMateFeat.Name);
                toFile.WriteLine("");
                // Get first mate, which is a subfeature
                swSubFeat = (Feature)swMateFeat.GetFirstSubFeature();
                while ((swSubFeat != null))
                {
                    swMate = (Mate2)swSubFeat.GetSpecificFeature2();
                    if ((swMate != null))
                    {
                        for (i = 0; i <= 1; i++)
                        {
                            swMateEnt[i] = swMate.MateEntity(i);
                            swComp       = swMateEnt[i].ReferenceComponent;
                            // Initate point
                            nPt[0] = 0.0;
                            nPt[1] = 0.0;
                            nPt[2] = 0.0;
                            vPt    = nPt;
                            // Get component origin point
                            swTrans    = swComp.Transform2;
                            swMathUtil = (MathUtility)swApp.GetMathUtility();
                            swOrig     = (MathPoint)swMathUtil.CreatePoint(vPt);
                            swOrig     = (MathPoint)swOrig.MultiplyTransform(swTrans);
                            // Write parameters to readable ASCII file
                            toFile.WriteLine("    " + swSubFeat.Name);
                            toFile.WriteLine("      Type              = " + swMate.Type);
                            toFile.WriteLine("      Alignment         = " + swMate.Alignment);
                            toFile.WriteLine("      Can be flipped    = " + swMate.CanBeFlipped);
                            toFile.WriteLine("");
                            toFile.WriteLine("      Component         = " + swComp.Name2);
                            toFile.WriteLine("      Origin            = (" + ((double[])swOrig.ArrayData)[0] * 1000.0 + ", " + ((double[])swOrig.ArrayData)[1] * 1000.0 + ", " + ((double[])swOrig.ArrayData)[2] * 1000.0 + ")");
                            toFile.WriteLine("      Mate enity type   = " + swMateEnt[i].ReferenceType);
                            entityParameters = (double[])swMateEnt[i].EntityParams;
                            toFile.WriteLine("      (x,y,z)           = (" + entityParameters[0] * 1000 + ", " + entityParameters[1] * 1000 + ", " + entityParameters[2] * 1000 + ")");
                            toFile.WriteLine("      (i,j,k)           = (" + entityParameters[3] + ", " + entityParameters[4] + ", " + entityParameters[5] + ")");
                            toFile.WriteLine("      Radius 1          = " + entityParameters[6] * 1000);
                            toFile.WriteLine("      Radius 2          = " + entityParameters[7] * 1000);
                            toFile.WriteLine("");
                            // Write parameters to a simplified ASCII file for computation
                            toCalc.Write(swSubFeat.Name);
                            toCalc.Write(" " + swMate.Type);
                            toCalc.Write(" " + swMate.Alignment);
                            toCalc.Write(" " + swMate.CanBeFlipped);
                            toCalc.Write(" " + swComp.Name2);
                            toCalc.Write(" " + ((double[])swOrig.ArrayData)[0] * 1000.0 + "," + ((double[])swOrig.ArrayData)[1] * 1000.0 + "," + ((double[])swOrig.ArrayData)[2] * 1000.0);
                            toCalc.Write(" " + swMateEnt[i].ReferenceType);
                            toCalc.Write(" " + entityParameters[0] * 1000.0 + "," + entityParameters[1] * 1000.0 + "," + entityParameters[2] * 1000.0);
                            toCalc.Write(" " + entityParameters[3] + "," + entityParameters[4] + "," + entityParameters[5]);
                            toCalc.Write(" " + entityParameters[6] * 1000.0);
                            toCalc.WriteLine(" " + entityParameters[7] * 1000.0);
                        }
                        toFile.WriteLine(" ");
                    }
                    // Get the next mate in MateGroup
                    swSubFeat = (Feature)swSubFeat.GetNextSubFeature();
                }
                // Get bounding box around assembly
                swAssembly = (AssemblyDoc)swModel;
                corners    = swAssembly.GetBox(1);
                height     = (corners[4] - corners[1]) * 1000.0;
                width      = (corners[3] - corners[0]) * 1000.0;
                depth      = (corners[5] - corners[2]) * 1000.0;
                // Write to file
                toFile.WriteLine("Aprx. assembly dimensions");
                toFile.WriteLine("(Height, Width, Depth) = (" + height + ", " + width + ", " + depth + ")");
                toFile.WriteLine(" ");
                toCalc.WriteLine("dims(hwd) " + height + " " + width + " " + depth);
                // Get Possible Assembly Directions with interferenceDir function
                interference inter = new interference();
                swAssyDir = inter.interferenceDir(swApp, swModel, swMateFeat, swMateEnt);
                // Write swAssyDir to file
                toFile.WriteLine("(x+, x-, y+, y-, z+, z-) = (" + swAssyDir[0] + ", " + swAssyDir[1] + ", " + swAssyDir[2] + ", " + swAssyDir[3] + ", " + swAssyDir[4] + ", " + swAssyDir[5] + ") ");
                toFile.WriteLine("1 is possible, 0 is not possible");
                toFile.WriteLine("");
                toFile.WriteLine("All dimensions are in mm");
                toCalc.Write("dir " + swAssyDir[0] + " " + swAssyDir[1] + " " + swAssyDir[2] + " " + swAssyDir[3] + " " + swAssyDir[4] + " " + swAssyDir[5]);
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Ejemplo n.º 2
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);
        }