Ejemplo n.º 1
0
        public static bool CalculateBallonCoordinate(CaxME.BoxCoordinate cBoxCoordinate, ref DimenData sDimenData)
        {
            try
            {
                //先取得欲偏移的方向向量
                double X2_X1   = (cBoxCoordinate.lower_left[0] - cBoxCoordinate.lower_right[0]);
                double Y2_Y1   = (cBoxCoordinate.lower_left[1] - cBoxCoordinate.lower_right[1]);
                double shift_X = (X2_X1) / Math.Sqrt(X2_X1 * X2_X1 + Y2_Y1 * Y2_Y1);
                double shift_Y = (Y2_Y1) / Math.Sqrt(X2_X1 * X2_X1 + Y2_Y1 * Y2_Y1);

                sDimenData.LocationX = (cBoxCoordinate.upper_left[0] + cBoxCoordinate.lower_left[0]) / 2;
                sDimenData.LocationY = (cBoxCoordinate.upper_left[1] + cBoxCoordinate.lower_left[1]) / 2;

                sDimenData.LocationX = sDimenData.LocationX + shift_X * 3;
                sDimenData.LocationY = sDimenData.LocationY + shift_Y * 3;

                sDimenData.LocationZ = 0;
            }
            catch (System.Exception ex)
            {
                return(false);
            }
            return(true);
        }
        private void InsertBalloon(GridRow row, CoordinateData cCoordinateData)
        {
            try
            {
                //取得圖紙長、高
                NXOpen.Drawings.DrawingSheet drawingSheet1 = (NXOpen.Drawings.DrawingSheet)workPart.DrawingSheets.FindObject(row.Cells["尺寸位置"].Value.ToString());
                drawingSheet1.Open();
                double SheetLength = drawingSheet1.Length;
                double SheetHeight = drawingSheet1.Height;

                //事先塞入該尺寸所在Sheet
                ((NXObject)row.Cells["Dimension"].Value).SetAttribute(CaxME.DimenAttr.SheetName, row.Cells["尺寸位置"].Value.ToString());
                //計算泡泡座標
                CaxME.BoxCoordinate cBoxCoordinate = new CaxME.BoxCoordinate();
                CaxME.GetTextBoxCoordinate(((NXOpen.Annotations.Annotation)row.Cells["Dimension"].Value).Tag, out cBoxCoordinate);
                DimenData sDimenData = new DimenData();
                Variables.CalculateBallonCoordinate(cBoxCoordinate, ref sDimenData);
                Point3d BallonLocation = new Point3d();
                BallonLocation.X = sDimenData.LocationX;
                BallonLocation.Y = sDimenData.LocationY;
                //決定數字的大小
                double BallonNumSize = 0;
                if (Convert.ToInt32(row.Cells["自定泡泡號"].Value) <= 9)
                {
                    BallonNumSize = 2.5;
                }
                else if (Convert.ToInt32(row.Cells["自定泡泡號"].Value) > 9 && Convert.ToInt32(row.Cells["自定泡泡號"].Value) <= 99)
                {
                    BallonNumSize = 1.5;
                }
                else
                {
                    BallonNumSize = 1;
                }
                NXObject balloonObj = null;
                CaxME.CreateBallonOnSheet(row.Cells["自定泡泡號"].Value.ToString(), BallonLocation, BallonNumSize, "BalloonAtt", out balloonObj);

                //取得該尺寸數量
                string diCount = "";
                try
                {
                    diCount = ((NXObject)row.Cells["Dimension"].Value).GetStringAttribute(CaxME.DimenAttr.DiCount);
                }
                catch (System.Exception ex)
                {
                    //當遇到舊料號沒有Dicount的屬性時,在這邊補上
                    ((NXObject)row.Cells["Dimension"].Value).SetAttribute(CaxME.DimenAttr.DiCount, "1");
                    diCount = "1";
                }
                //如果大於1表示要插入a.b.c.....
                if (diCount != "1")
                {
                    //文字座標
                    CaxME.BoxCoordinate sBoxCoordinate = new CaxME.BoxCoordinate();
                    CaxME.GetTextBoxCoordinate(balloonObj.Tag, out sBoxCoordinate);
                    Point3d textCoord = new Point3d(sBoxCoordinate.lower_left[0] + 1.5, sBoxCoordinate.lower_left[1] - 1.5, 0);
                    string  countText = Convert.ToChar(65 + 0).ToString().ToLower() + "-" + Convert.ToChar(65 + Convert.ToInt32(diCount) - 1).ToString().ToLower();
                    CaxME.InsertDicountNote(row.Cells["自定泡泡號"].Value.ToString(), CaxME.DimenAttr.DiCount, countText, "1.8", textCoord);
                }

                #region 計算泡泡相對位置
                string RegionX = "", RegionY = "";
                for (int ii = 0; ii < cCoordinateData.DraftingCoordinate.Count; ii++)
                {
                    string SheetSize = cCoordinateData.DraftingCoordinate[ii].SheetSize;
                    if (Math.Ceiling(SheetHeight) != Convert.ToDouble(SheetSize.Split(',')[0]) || Math.Ceiling(SheetLength) != Convert.ToDouble(SheetSize.Split(',')[1]))
                    {
                        continue;
                    }
                    //比對X
                    for (int j = 0; j < cCoordinateData.DraftingCoordinate[ii].RegionX.Count; j++)
                    {
                        string X0 = cCoordinateData.DraftingCoordinate[ii].RegionX[j].X0;
                        string X1 = cCoordinateData.DraftingCoordinate[ii].RegionX[j].X1;
                        if (BallonLocation.X >= Convert.ToDouble(X0) && BallonLocation.X <= Convert.ToDouble(X1))
                        {
                            RegionX = cCoordinateData.DraftingCoordinate[ii].RegionX[j].Zone;
                        }
                    }
                    //比對Y
                    for (int j = 0; j < cCoordinateData.DraftingCoordinate[ii].RegionY.Count; j++)
                    {
                        string Y0 = cCoordinateData.DraftingCoordinate[ii].RegionY[j].Y0;
                        string Y1 = cCoordinateData.DraftingCoordinate[ii].RegionY[j].Y1;
                        if (BallonLocation.Y >= Convert.ToDouble(Y0) && BallonLocation.Y <= Convert.ToDouble(Y1))
                        {
                            RegionY = cCoordinateData.DraftingCoordinate[ii].RegionY[j].Zone;
                        }
                    }
                }
                #endregion

                ((NXObject)row.Cells["Dimension"].Value).SetAttribute(CaxME.DimenAttr.BallonNum, row.Cells["自定泡泡號"].Value.ToString());
                ((NXObject)row.Cells["Dimension"].Value).SetAttribute(CaxME.DimenAttr.BallonLocation, row.Cells["尺寸位置"].Value.ToString() + "-" + RegionY + RegionX);
                workPart.Views.Refresh();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }