예제 #1
0
        private void buttonAuto_Click(object sender, EventArgs e)
        {
            //新建一个零件  ,画一条150长度的直线,然后最大化.

            ISldWorks iswApp = Utility.ConnectToSolidWorks();

            string partDefaultPath = iswApp.GetDocumentTemplate(1, "", 0, 0, 0);

            var part = iswApp.NewDocument(partDefaultPath, 0, 0, 0);

            ModelDoc2 modelDoc = (ModelDoc2)iswApp.ActiveDoc;

            modelDoc.Extension.SelectByID2("Plane1", "PLANE", 0, 0, 0, false, 0, null, 0);

            modelDoc.SketchManager.InsertSketch(true);

            modelDoc.SketchManager.CreateLine(0, 0.1, 0, 0, 0, 0);

            modelDoc.SketchManager.InsertSketch(true);

            modelDoc.ClearSelection2(true);

            modelDoc.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, false, 1, null, 0);

            var b = modelDoc.InsertCompositeCurve();

            modelDoc.ClearSelection2(true);

            modelDoc.FeatureManager.ViewFeatures = false;

            modelDoc.ViewZoomtofit2();

            ModelView modelView = (ModelView)modelDoc.ActiveView;

            iswApp.FrameState = 1; //最大化solidworks
            modelDoc.ViewZoomtofit2();
            modelView.Scale2 = 0.5;

            //

            modelDoc.ClearSelection2(true);

            string ImagePath = @"D:\temp.JPG";

            modelDoc.SaveAs3(ImagePath, 0, 0);

            int LineInImage = img2color(ImagePath);

            try
            {
                System.IO.File.Delete(ImagePath);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }

            if (LineInImage > 0)
            {
                double ActionScreenHeight = double.Parse(txtSCHeight.Text); //GetScreenRealWeight(double.Parse( textScreenSize.Text));

                //当前视图的比例
                double ActionScale = modelView.Scale2;

                //solidworks中的直线占比
                double defaultSolidworksP = LineInImage / 1200.0;//double.Parse(txtScreenY.Text);

                //当前比例下 1像素  等于多少mm
                double thisSc = 100 / LineInImage;

                //实际屏幕 1 像素尺寸

                double oneP = (ActionScreenHeight) / 1200.0;

                //当比例设置为 1时 1像素能表示的长度:
                double onePshowLength = thisSc / ActionScale;

                //959  1370

                GetWindowsSize getWindowSize = new GetWindowsSize();

                GetWindowsSize.windsize solidworksize = getWindowSize.GetSize("SLDWORKS");

                double getSC = 50.0 / (LineInImage * oneP);

                txtSC.Text = getSC.ToString();// getSC.ToString();

                modelView.Scale2 = getSC;

                modelDoc.GraphicsRedraw2();

                Debug.Print(getSC.ToString());
                // modelView.Scale2 = double.Parse(txtSC.Text);
                // sldWorks.scen
            }

            iswApp.CloseDoc(modelDoc.GetPathName());
        }