Code optimization

Code optimization

jamess166
Advocate Advocate
860 Views
3 Replies
Message 1 of 4

Code optimization

jamess166
Advocate
Advocate

Good day, get the parameters of the concrete elements and assign it to all the bars that are housed in it, the code I have works, however it takes its time to load, I get the impression that there are other ways to do it so that I consume less resources. I appreciate your help, thank you.

 

namespace RebarParameters
{
    class Parametros
    {
        //Instancio a Longitu de Rebar
        LongitudRebar longitudRebar = new LongitudRebar();
        
        //Devuelve Parametro compartido
        public Parameter Parameter(Element e, String guid)
        {
            Guid parameterShared = new Guid(guid);
            Parameter parameter = e.get_Parameter(parameterShared);
            return parameter;
        }

        public Parameter Parameter(Element e, String lookParameter,Boolean boolean)
        {            
            Parameter parameter = e.LookupParameter(lookParameter);
            return parameter;
        }

        //Devuelve Parametro BuiltInParameter
        public Parameter Parameter(Element e, BuiltInParameter parameterBuild)
        {
            Parameter parameter = e.get_Parameter(parameterBuild);
            return parameter;
        }

        //Devuelve Parametro de tipo BuiltInParameter
        public Parameter Parameter(ElementType et, BuiltInParameter parameterBuild)
        {
            Parameter parameter = et.get_Parameter(parameterBuild);
            return parameter;
        }

        //Devuelve parametro Double 0 si existe error
        public Double ConvertirDouble(Parameter parameter)
        {
            Double dimension;
            try
            {
                dimension = parameter.AsDouble();
            }
            catch (Exception)
            {
                dimension = 0;
            }
            return dimension;
        }
                
        public void GetSetParameterRebar(List<Element> listHost, Document doc)
        {           
            //Metodo parametros
            foreach (Element el in listHost)
            {
                //Parametro de Host
                ElementType HostType = doc.GetElement(el.GetTypeId()) as ElementType;
                Parameter HostTypeComments = Parameter(HostType, BuiltInParameter.ALL_MODEL_TYPE_COMMENTS);
                Parameter HostMark = Parameter(el, "1411f266-da7a-4faa-beb9-3848e3304161");
                Parameter QC_HostSector = Parameter(el, "0818baf0-dce1-4ea4-84ac-2f69fae50a84");
                Parameter QC_HostNivel = Parameter(el, "d25405d0-37af-43c2-be2d-7efc1c792fcc");
                Parameter QC_HostEje = Parameter(el, "2ee543a4-ec44-472e-80c4-520883abe8c0");
                Parameter QC_HostFamilia = Parameter(el, "8ca45e69-853c-4132-9452-8124c3b0f3ef");

                //rebar in host
                IList<Rebar> rebar = RebarHostData.GetRebarHostData(el).GetRebarsInHost();    

                if (rebar.Count != 0)
                {
                    // para cada rebar segun su host
                    List<String> formas = new List<string>();

                    foreach (Rebar r in rebar)
                    {
                        //Parametros Rebar de Ubicacion y Sectorización
                        Parameter partition = Parameter(r, BuiltInParameter.NUMBER_PARTITION_PARAM);
                        Parameter QC_HostMark = Parameter(r, "3be2f711-4769-463b-bda8-39ba39aa578d");
                        Parameter QC_Sector = Parameter(r, "0818baf0-dce1-4ea4-84ac-2f69fae50a84");
                        Parameter QC_Nivel = Parameter(r, "d25405d0-37af-43c2-be2d-7efc1c792fcc");
                        Parameter QC_Eje = Parameter(r, "2ee543a4-ec44-472e-80c4-520883abe8c0");
                        Parameter QC_Familia = Parameter(r, "8ca45e69-853c-4132-9452-8124c3b0f3ef");

                        //Parametros de Dimensiones
                        Parameter ParQC_RebarLength = Parameter(r, "8acbe253-9336-4934-925d-7e2dff0be89d");
                        Parameter ParA = Parameter(r, "A", true);
                        Parameter ParB = Parameter(r, "B", true);
                        Parameter ParC = Parameter(r, "C", true);
                        Parameter ParD = Parameter(r, "D", true);
                        Parameter ParE = Parameter(r, "E", true);
                        Parameter ParF = Parameter(r, "F", true);
                        Parameter ParG = Parameter(r, "G", true);
                        Parameter ParH = Parameter(r, "H", true);
                        Parameter ParI = Parameter(r, "I", true);
                        Parameter ParAlfa = Parameter(r, "fb540c64-8611-416a-9172-681a1847112d");
                        Parameter ParBeta = Parameter(r, "b4aa2db2-c434-4457-88a0-f097a6efd159");
                        Parameter ParGamma = Parameter(r, "da72b204-3415-4298-bc87-3c07fb7b8ce4");
                        Parameter ParQC_Codigo_Forma = Parameter(r, "f1fb3a75-4c81-4cd7-980d-7cc02d561a6c");

                        //definicion de parametros de longitud   
                        String QC_Codigo_Forma = ParQC_Codigo_Forma.AsString();
                        Double a = ConvertirDouble(ParA);
                        Double b = ConvertirDouble(ParB);
                        Double c = ConvertirDouble(ParC);
                        Double d = ConvertirDouble(ParD);
                        Double e = ConvertirDouble(ParE);
                        Double f = ConvertirDouble(ParF);
                        Double g = ConvertirDouble(ParG);
                        Double h = ConvertirDouble(ParH);
                        Double i = ConvertirDouble(ParI);
                        Double alfa = ConvertirDouble(ParAlfa);
                        Double beta = ConvertirDouble(ParBeta);
                        Double gamma = ConvertirDouble(ParGamma);

                        //Calcula la longitud
                        Double LongPieza = longitudRebar.LongitudBarra(QC_Codigo_Forma, a, b, c, d, e, f, g, h, i, alfa, beta, gamma);

                        if (LongPieza == 0)
                        {
                            formas.Add(QC_Codigo_Forma);
                        }

                        using (Transaction t = new Transaction(doc, "Parametros"))
                        {
                            t.Start("Parametros");
                            //Asignar Parametros a rebar                            
                            SetParameter(HostTypeComments, partition);
                            SetParameter(HostMark, QC_HostMark);
                            SetParameter(QC_HostSector, QC_Sector);
                            SetParameter(QC_HostNivel, QC_Nivel);
                            SetParameter(QC_HostEje, QC_Eje);
                            SetParameter(QC_HostFamilia, QC_Familia);
                            SetParameter(LongPieza, ParQC_RebarLength);
                            t.Commit();
                        }
                    }

                    if (formas.Count != 0)
                    {
                        String infoformas = "No se encuentra registrado el código de la forma: ";

                        foreach (string f in formas.Distinct())
                        {
                            infoformas += "\n\t" + f;
                        }
                        TaskDialog.Show("QC Ingenieros", infoformas);
                    }

                }
                else
                {                    
                    TaskDialog.Show("QC Ingenieros", "No se ha encontrado ningun elemento de refuerzo en el elemento: " + el.Id.ToString());
                }

            }
        }

        private void CounColorNumber(List<Rebar> list)
        {
            foreach (Rebar r in list)
            {

            }
        }

        private void SetParameter (Parameter HostParameter,Parameter RebarParameter)
        {
            try
            {
                RebarParameter.Set(HostParameter.AsString());

            }
            catch (Exception)
            {
                              
            }
        }

        private void SetParameter(Double HostParameter, Parameter RebarParameter)
        {
            try
            {
                RebarParameter.Set(HostParameter);

            }
            catch (Exception)
            {

            }
        }

    }
}
0 Likes
Accepted solutions (1)
861 Views
3 Replies
Replies (3)
Message 2 of 4

jeremytammik
Autodesk
Autodesk
Accepted solution

The one thing that comes to mind at first glance is the following:

  

Eliminate the two parameter lookup methods taking string arguments.

  

Where the string represents a GUID, you can create all the GUID instances from the strings up front, before starting the loop.

  

That will save the time and effort of recreating them many times over.

 

Similarly, for the calls to LookupParameter taking a string: determine the Parameter id of each corresponding parameter definition once and for all beforehand, before starting the loop. Then, you can use Element.get_Parameter(Parameter) instead of LookupParameter.

 

Once that is done, the next step might be to profile your code:

 

https://stackoverflow.com/questions/3927/what-are-some-good-net-profilers

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 4

jamess166
Advocate
Advocate

I appreciate your help, with the little I know of programming my code ended as follows:

 

public void GetSetParameterRebar(List<Element> listHost, Document doc)
        {
            //Metodo parametros
            foreach (Element el in listHost)
            {
                //creacion de parametros guid host
                Guid guidHostMark = new Guid("1411f266-da7a-4faa-beb9-3848e3304161");
                Guid guidSector = new Guid("0818baf0-dce1-4ea4-84ac-2f69fae50a84");
                Guid guidNivel = new Guid("d25405d0-37af-43c2-be2d-7efc1c792fcc");
                Guid guidEje = new Guid("2ee543a4-ec44-472e-80c4-520883abe8c0");
                Guid guidHostFamilia = new Guid("8ca45e69-853c-4132-9452-8124c3b0f3ef");

                //creacion de parametros guid rebar
                Guid guidParQC_RebarLength = new Guid("8acbe253-9336-4934-925d-7e2dff0be89d");
                Guid guidParQC_Codigo_Forma = new Guid("f1fb3a75-4c81-4cd7-980d-7cc02d561a6c");
                BuiltInParameter buildPartition = BuiltInParameter.NUMBER_PARTITION_PARAM;
                
                //Parametro de Host
                ElementType HostType = doc.GetElement(el.GetTypeId()) as ElementType;
                Parameter HostTypeComments = HostType.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_COMMENTS);
                Parameter QC_HostMark = el.get_Parameter(guidHostMark);
                Parameter QC_HostSector = el.get_Parameter(guidSector);
                Parameter QC_HostNivel = el.get_Parameter(guidNivel);
                Parameter QC_HostEje = el.get_Parameter(guidEje);
                Parameter QC_HostFamilia = el.get_Parameter(guidHostFamilia);    

                //rebar in host
                IList <Rebar> rebar = RebarHostData.GetRebarHostData(el).GetRebarsInHost();


                if (rebar.Count != 0)
                {
                    // para cada rebar segun su host
                    List<String> formas = new List<string>();

                    foreach (Rebar r in rebar)
                    {
                        //Parametros Rebar de Ubicacion y Sectorización
                        Parameter partition = r.get_Parameter(buildPartition);
                        Parameter QC_RbHostMark = r.get_Parameter(guidHostMark);
                        Parameter QC_Sector = r.get_Parameter(guidSector);
                        Parameter QC_Nivel = r.get_Parameter(guidNivel);
                        Parameter QC_Eje = r.get_Parameter(guidEje);
                        Parameter QC_Familia = r.get_Parameter(guidHostFamilia);

                        ////Parametros de Dimensiones
                        Parameter ParQC_RebarLength = r.get_Parameter(guidParQC_RebarLength);
                        Parameter ParQC_Codigo_Forma = r.get_Parameter(guidParQC_Codigo_Forma);

                        ////definicion de parametros de longitud   
                        String QC_Codigo_Forma = ParQC_Codigo_Forma.AsString();


                        //Calcula la longitud
                        Double LongPieza = longitudRebar.LongitudBarra(doc,r,QC_Codigo_Forma);

                        if (LongPieza == 0)
                        {
                            formas.Add(QC_Codigo_Forma);
                        }

                        using (Transaction t = new Transaction(doc, "Parametros"))
                        {
                            t.Start("Parametros");
                            //Asignar Parametros a rebar                            
                            SetParameter(HostTypeComments, partition);
                            SetParameter(QC_RbHostMark, QC_RbHostMark);
                            SetParameter(QC_HostSector, QC_Sector);
                            SetParameter(QC_HostNivel, QC_Nivel);
                            SetParameter(QC_HostEje, QC_Eje);
                            SetParameter(QC_HostFamilia, QC_Familia);
                            SetParameter(LongPieza, ParQC_RebarLength);
                            t.Commit();
                        }
                    }

                    if (formas.Count != 0)
                    {
                        String infoformas = "No se encuentra registrado el código de la forma: ";

                        foreach (string f in formas.Distinct())
                        {
                            infoformas += "\n\t" + f;
                        }
                        TaskDialog.Show("QC Ingenieros", infoformas);
                    }

                    //}
                    else
                    {
                        TaskDialog.Show("QC Ingenieros", "No se ha encontrado ningun elemento de refuerzo en el elemento: " + el.Id.ToString());
                    }

                }
            }
        }

what I have not been able to do until now is to obtain the value of the Style parameter and apply an if it is Standard or Stirrup.

Captura.PNG

0 Likes
Message 4 of 4

jamess166
Advocate
Advocate

Regarding the additional question I have achieved it after doing several tests.

 

//obtener shape
            RebarShape rebarShape = doc.GetElement(r.GetShapeId()) as RebarShape;

            RebarStyle rb = rebarShape.RebarStyle;

if (rb.ToString() is "Standard")
            {
                TaskDialog.Show("QC", rb.ToString());
            }  
0 Likes