Implement multiple interface in Vault customization

By Xiaodong Liang

(Continued)

In the past post, we got a skeleton of a translator add-in. By this translator, we can open a data source *.abc, and save the geometries of Inventor to a *.abc file.

To make a simpler demo, we define the *.abc as a text file. It has 3 lines:
  1. Three double, split by the comma (for center point of a sphere)
  2. A double (for radius of a sphere)
  3. A string (for iProperties >> part number)

In Open method of the add-in. we will open a *.abc file, create a sphere with data of line 1 & 2, and update iProperties >> part number with line 3. In the SaveCopyAs method, we will get one existing sphere face of a part, save center point coordinates and radius to the *.abc file. And save iProperties >> part number to the file.

public void Open(DataMedium SourceData,
                 TranslationContext Context,
                 NameValueMap Options,
                 ref object TargetObject)
{
    // When Open *.abc, this function will
    // be invoked

    //the file name of the data source
    string fileName = SourceData.FileName;

    // the params read from the data source

    //center point of sphere
    Inventor.Point oCenterPt =
        m_inventorApplication.TransientGeometry.CreatePoint();
    //radius of sphere
    double radius = 0;
    //part number
    string partNumber = "";

    try
    {
        //read the data source
        using (StreamReader sr = new StreamReader(fileName))
        {
            string line;
            int rowIndex = 0;
            // Read and display lines from the file until the end of
            // the file is reached.
            while ((line = sr.ReadLine()) != null)
            {
                if (rowIndex == 0)
                {
                    //center point of sphere
                    string[] oStrArray = line.Split(new char[]{','});
                    oCenterPt.X = Convert.ToDouble(oStrArray[0]);
                    oCenterPt.Y = Convert.ToDouble(oStrArray[1]);
                    oCenterPt.Z = Convert.ToDouble(oStrArray[2]);
                }
                else if (rowIndex == 1)
                {
                    //radius of sphere
                    radius = Convert.ToDouble(line);
                }
                else if(rowIndex == 2)
                {
                    //part number
                    partNumber = line;
                }
                else
                {
                    //in this demo, we are interested in
                    // the 3 params only
                    break;
                }

                rowIndex++;
            }
        }

        try
        {
            if (radius > 0)
            {
                //if the radius is valid

                //create a new part document
                PartDocument oNewDoc =
                    m_inventorApplication.Documents.Add(
                        DocumentTypeEnum.kPartDocumentObject)
                    as PartDocument;
                //get ComponentDefinition of the part
                PartComponentDefinition oDocDef =
                    oNewDoc.ComponentDefinition;
                //get TransientBRep
                TransientBRep oTB =
                    m_inventorApplication.TransientBRep;
                //create a transient body of a sphere
                SurfaceBody oSB =
                    oTB.CreateSolidSphere(oCenterPt, radius);
                //add the transient body to the part as a
                // non ParametricBaseFeature
                oDocDef.Features
                       .NonParametricBaseFeatures.Add(oSB);
                //update the iProperties with the part
                //number from the data source
                Inventor.PropertySet oPS =
                    oNewDoc.PropertySets["Design Tracking Properties"];
                oPS.ItemByPropId[
                    (int)PropertiesForDesignTrackingPropertiesEnum.
                        kPartNumberDesignTrackingProperties].Value = partNumber;

                m_inventorApplication.ActiveView.Fit();
            }
            else
            {
                MessageBox.Show("the radius is invalid!");
            }
        }
        catch (Exception Exception1)
        {
            MessageBox.Show("fail to create model!\n" +
                            Exception1.ToString());
        }
    }
    catch(Exception Exception)
    {
        MessageBox.Show("reading data failed!\n" +
                        Exception.ToString());
    }
}
public void SaveCopyAs(object SourceObject,
                       TranslationContext Context,
                       NameValueMap Options,
                       DataMedium TargetData)
{
    // When save to *.abc, this function will
    // be invoked

    //the file to be saved
    string fileName = TargetData.FileName;

    PartDocument oPartDoc =
        m_inventorApplication.ActiveDocument
        as PartDocument;

    PartComponentDefinition oPartDef =
        oPartDoc.ComponentDefinition;

    if (oPartDef.Features.RevolveFeatures.Count > 0)
    {
        //if there is revolve feature in the part

        //get one revolve feature
        //which is full sphere surface

        bool found = false;
        Inventor.Point centerPt =
            m_inventorApplication.TransientGeometry.CreatePoint();

        double radius = 0;
        string partNumber = "";

        foreach (RevolveFeature oRF
                 in oPartDef.Features.RevolveFeatures)
        {
            if (oRF.Faces.Count == 1)
            {
                Face oF = oRF.Faces[1];
                if(oF.Edges.Count == 0 &&
                   oF.Vertices.Count == 0)
                {
                    //this is a full sphere
                    found = true;
                    SurfaceEvaluator oSE = oF.Evaluator;
                    //calculate radius
                    radius = Math.Sqrt(oSE.Area / (4 * Math.PI));
                    //calcuate center point (corrected from original source for proper midpoint)
                    Inventor.Point maxPt = oSE.RangeBox.MaxPoint;
                    Inventor.Point minPt = oSE.RangeBox.MinPoint;
                    centerPt.X = (maxPt.X + minPt.X) / 2.0;
                    centerPt.Y = (maxPt.Y + minPt.Y) / 2.0;
                    centerPt.Z = (maxPt.Z + minPt.Z) / 2.0;
                }
            }
        }

        if (!found)
        {
            MessageBox.Show("no revolve feature which is full sphere!");
            return;
        }

        //get the iProperties >> part number
        Inventor.PropertySet oPS =
            oPartDoc.PropertySets["Design Tracking Properties"];
        partNumber = oPS.ItemByPropId[
            (int)PropertiesForDesignTrackingPropertiesEnum.
                kPartNumberDesignTrackingProperties].Value.ToString();

        //write the data to the *.abc
        using (StreamWriter writer =
               new StreamWriter(fileName))
        {
            string centerPtStr = centerPt.X + "," +
                                 centerPt.Y + "," +
                                 centerPt.Z;

            writer.WriteLine(centerPtStr);
            writer.WriteLine(radius.ToString());
            writer.WriteLine(partNumber);
        }
    }
    else
    {
        MessageBox.Show("no revolve feature!");
    }
}

The full project can be downloaded at: Download MyTranslator

UntitledAs you remember, we can customize the options of the built-in translators. It is same to custom translator add-in.In the demo above, we define the behaviors directly without taking any options into account. In the next post, we will see how to control the options.

(to be continued)


Comments

One response to “Implement multiple interface in Vault customization”

  1. I want to say thank you to great izzakpa doo for everything so far.
    To everyone who doesn’t believe in spell, I was one of
    those ones at first. I wasn’t quite sure if I wanted to do
    this since I’ve tried others so-called spells casters and they
    did not work and was a waste of my time and money.
    However, when I read through the testimonials of other people at this
    website and after I talked to great izzakpa doo , who
    answered all my questions and was very nice about
    everything, I decided to give it a try. I figured it would be
    my last try to get my guy back. So my story is that I was
    in my office when the guy I am in love with told me that he
    wasn’t in love with me and never will he be and that he didn’t want to
    speak or see me again, especially since he
    was talking to this other girl. When I talked to great izzakpa doo, He
    let me know which spells would be most appropriate for me
    to use in this case and I chose the ones that was to get
    him back to me and stay with me and want to marry me.As
    soon as he started on the spells, my guy came back into my life! It was a
    miracle to me and I’m so thankful for that.
    Things have been going well, and pretty much according to
    what great izzakpa doo said it will happen. He’s always there when
    you need him and that’s also after the spell is done. But
    with all that has happened so far I’m very happy because
    given only four weeks ago, if you asked me or my friends if I would have
    anticipated how things were right no..No one
    would believe it! My name is Micky. contact his email
    address: izzakpadoosoluctionhome@outlook.com
    I want to say my big thanks to great izzakpa doo
    (1)If you want your ex back.
    (2) if you always have bad dreams.
    (3)You want to be promoted in your office.
    (4)You want women/men to run after you.
    (5)If you want a child.
    (6)[You want to be rich.
    (7)You want to tie your husband/wife to be yours forever.
    (8)If you need financial assistance.
    (9)Herbal care
    10)Help bringing people out of prison
    (11)Marriage Spells
    (12)Miracle Spells
    (13)Beauty Spells
    (14)PROPHECY CHARM
    (15)Attraction Spells
    (16)Evil Eye Spells
    (17)Kissing Spell
    (18)Remove Sickness Spells
    (19)ELECTION WINNING SPELLS
    (20)SUCCESS IN EXAMS SPELLS
    (21) Charm to get who to love you.
    (22)Business spell.
    Contact him today on:
    izzakpadoosoluctionhome@outlook.com

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading