C# getting the sizes of component

C# getting the sizes of component

nguyenduyanh82
Explorer Explorer
431 Views
0 Replies
Message 1 of 1

C# getting the sizes of component

nguyenduyanh82
Explorer
Explorer

I'm writing an Add-in by C# with the method "SurfaceBody.OrientedMinimumRangeBox". My add-in have two methods to get the sizes: Global UCS and Custom UCS. My form have a "ComboBox" with 2 options: "Manual" and "Auto"  So I have a problem with "ClientGraphics". Below are code and picture:

 

nguyenduyanh82_2-1631018617775.jpeg

nguyenduyanh82_3-1631018633407.jpeg

 

   public partial class Form2 : Form
    {
        private Inventor.Application _invApp;
        private Inventor.PartDocument _partDoc;
        private Transaction transac;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            if (ConnectInventor())
            {
                cmbMethod.SelectedIndex = 1;

                UpdateCustomiProperty("FThick", 0);
                UpdateCustomiProperty("FWidth", 0);
                UpdateCustomiProperty("FLength", 0);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if(transac != null)
            {
                transac.Abort();
                _invApp.ActiveView.Update();
            } 
            Close();
        }

        private bool ConnectInventor()
        {
            try
            {
                try
                {
                    //Get active Inventor object
                    _invApp = System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application") as Inventor.Application;
                }
                catch (COMException)
                {
                    MessageBox.Show("Inventor must be running.");
                    return false;
                }

                //Make sure that at lease one document is opened
                _partDoc = _invApp.ActiveDocument as PartDocument;
                if (_partDoc == null)
                {
                    MessageBox.Show("A assembly document must be active.");
                    return false;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return false;
            }

            return true;
        }

        private void lblSwapA_Click(object sender, EventArgs e)
        {
            string temp = txtFThick.Text;
            txtFThick.Text = txtFWidth.Text;
            txtFWidth.Text = temp;
        }

        private void lblSwapB_Click(object sender, EventArgs e)
        {
            string temp = txtFWidth.Text;
            txtFWidth.Text = txtFLength.Text;
            txtFLength.Text = temp;
        }

        private void UpdateCustomiProperty(string PropertyName, double PropertyValue)
        {
            // Get the custom property set
            PropertySet customPropSet = _partDoc.PropertySets["Inventor User Defined Properties"];

            // Get the existing property, if it exists
            Property prop = null;

            try
            {
                prop = customPropSet[PropertyName];
            }
            catch
            {
                customPropSet.Add(PropertyValue, PropertyName);
            }
        }

        private void cmbMethod_SelectedIndexChanged(object sender, EventArgs e)
        {
            TransientBRep transBRep = _invApp.TransientBRep;
            TransientGeometry transGeom = _invApp.TransientGeometry;

            double dir1, dir2, dir3, xLength, yLength, zLength;

            OrientedBox alignedRangeBox;

            UnitsOfMeasure uom = _partDoc.UnitsOfMeasure;

            if (cmbMethod.SelectedIndex == 0)
            {
                txtFThick.Enabled = false;
                txtFWidth.Enabled = false;
                txtFLength.Enabled = false;

                // Combine all bodies in Part into a single transient Surface Body
                SurfaceBody combinedBodies = null;
                foreach (SurfaceBody surfBody in _partDoc.ComponentDefinition.SurfaceBodies)
                {
                    if (combinedBodies == null)
                    {
                        combinedBodies = transBRep.Copy(surfBody);
                    }
                    else
                    {
                        transBRep.DoBoolean(combinedBodies, surfBody, BooleanTypeEnum.kBooleanTypeUnion);
                    }
                }

                // Get the oriented minium range box of all boides in Part
                alignedRangeBox = combinedBodies.OrientedMinimumRangeBox;
            }
            else
            {
                txtFThick.Enabled = true;
                txtFWidth.Enabled = true;
                txtFLength.Enabled = true;

                SurfaceBody oBody = _partDoc.ComponentDefinition.SurfaceBodies[1];
                UserCoordinateSystem oUCS = _partDoc.ComponentDefinition.UserCoordinateSystems[1];

                // Create tranisent copy of body
                SurfaceBody transientBody = transBRep.Copy(oBody);

                // Get vectors aligned with UCS
                Vector ucsXAxis = WorkAxisToVector(oUCS.XAxis);
                Vector ucsYAxis = WorkAxisToVector(oUCS.YAxis);
                Vector ucsZAxis = WorkAxisToVector(oUCS.ZAxis);

                // Create transformation matrix from UCS to WCS
                Matrix UCSToWCSMatrix = oUCS.Transformation;

                UCSToWCSMatrix.SetToAlignCoordinateSystems(FromOrigin: oUCS.Origin.Point, FromXAxis: ucsXAxis, FromYAxis: ucsYAxis, FromZAxis: ucsZAxis, ToOrigin: transGeom.CreatePoint(), ToXAxis: transGeom.CreateVector(1, 0, 0), ToYAxis: transGeom.CreateVector(0, 1, 0), ToZAxis: transGeom.CreateVector(0, 0, 1));

                // Transform transientbody from UCS to WCS
                transBRep.Transform(transientBody, UCSToWCSMatrix);

                // Create transformation matrix from WCS to UCS
                Matrix WCSToUCSMatrix = UCSToWCSMatrix.Copy();
                WCSToUCSMatrix.Invert();

                // Transform corner point of rangebox from WCS to UCS
                Point cornerPoint = transientBody.RangeBox.MinPoint.Copy();
                cornerPoint.TransformBy(WCSToUCSMatrix);

                // Scale UCS vectors to match dimensions of rangebox
                ucsXAxis.ScaleBy(transientBody.RangeBox.MaxPoint.X - transientBody.RangeBox.MinPoint.X);
                ucsYAxis.ScaleBy(transientBody.RangeBox.MaxPoint.Y - transientBody.RangeBox.MinPoint.Y);
                ucsZAxis.ScaleBy(transientBody.RangeBox.MaxPoint.Z - transientBody.RangeBox.MinPoint.Z);

                alignedRangeBox = transGeom.CreateOrientedBox(CornerPoint: cornerPoint, DirectionOne: ucsXAxis, DirectionTwo: ucsYAxis, DirectionThree: ucsZAxis);
            }

            // Get length of each side of mininum range box
            dir1 = alignedRangeBox.DirectionOne.Length;
            dir2 = alignedRangeBox.DirectionTwo.Length;
            dir3 = alignedRangeBox.DirectionThree.Length;

            // Convert lengths to docuemnt's units
            dir1 = uom.ConvertUnits(dir1, "cm", uom.LengthUnits);
            dir2 = uom.ConvertUnits(dir2, "cm", uom.LengthUnits);
            dir3 = uom.ConvertUnits(dir3, "cm", uom.LengthUnits);

            // Sort lengths form smallest to largest
            List<double> lengths = new List<double>();
            lengths.Add(dir1);
            lengths.Add(dir2);
            lengths.Add(dir3);

            lengths.Sort();

            xLength = lengths[2];
            yLength = lengths[1];
            zLength = lengths[0];

            //// Create surface to represent oriented range box...
            // Create starting box with dimensions of oriented rangebox.
            Box startBox = transGeom.CreateBox();
            Point boxMaxPoint = transGeom.CreatePoint(alignedRangeBox.DirectionOne.Length, alignedRangeBox.DirectionTwo.Length, alignedRangeBox.DirectionThree.Length);
            startBox.Extend(boxMaxPoint);

            // Create transformation matrix to move box to correct location/orienttation
            Matrix transMatrix = _invApp.TransientGeometry.CreateMatrix();
            transMatrix.SetCoordinateSystem(alignedRangeBox.CornerPoint, alignedRangeBox.DirectionOne.AsUnitVector().AsVector(), alignedRangeBox.DirectionTwo.AsUnitVector().AsVector(), alignedRangeBox.DirectionThree.AsUnitVector().AsVector());

            // Create surface body for the range box
            SurfaceBody minBoxSurface = _invApp.TransientBRep.CreateSolidBlock(startBox);

            // Transform range box surface body to the correct loction/orientation
            _invApp.TransientBRep.Transform(minBoxSurface, transMatrix);

            // Display range box on screen                    
            transac = _invApp.TransactionManager.StartTransaction((_Document)_partDoc, "DisplayRangeBox");

            ClientGraphics cGraphics = _partDoc.ComponentDefinition.ClientGraphicsCollection.Add("OrientedRangeBox");

            GraphicsNode surfacesNode = cGraphics.AddNode(1);
            surfacesNode.AddSurfaceGraphics(minBoxSurface);
            Asset targetAppearance;

            try
            {
                targetAppearance = _partDoc.Assets["Clear - Blue"];
            }
            catch
            {
                Asset sourceAppearance = _invApp.AssetLibraries["314DE259-5443-4621-BFBD-1730C6CC9AE9"].AppearanceAssets["InvGen-001-1-2"];
                targetAppearance = sourceAppearance.CopyTo(_partDoc);
            }

            surfacesNode.Appearance = targetAppearance;
            _invApp.ActiveView.Update();        

            txtFThick.Text = zLength.ToString("#.#");
            txtFWidth.Text = yLength.ToString("#.#");
            txtFLength.Text = xLength.ToString("#.#");
        }

        private Vector WorkAxisToVector(WorkAxis oWorkAxis)
        {
            var p = oWorkAxis.Line.Direction;
            return _invApp.TransientGeometry.CreateVector(p.X, p.Y, p.Z);
        }
    }

 

Thanks

0 Likes
432 Views
0 Replies
Replies (0)