Change Linestyle parameter in api

Change Linestyle parameter in api

Anonymous
Not applicable
6,981 Views
7 Replies
Message 1 of 8

Change Linestyle parameter in api

Anonymous
Not applicable

Is it possible to change the Linestyle parameter of a line from one linestyle to another in the api?

0 Likes
6,982 Views
7 Replies
Replies (7)
Message 2 of 8

jeremytammik
Autodesk
Autodesk

Sure. Use the CurveElement.LineStyle property. Here is the explanation, from 2010, still valid:

 

http://thebuildingcoder.typepad.com/blog/2010/01/model-and-detail-curve-colour.html

 

Cheers,

 

Jeremy



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

0 Likes
Message 3 of 8

Anonymous
Not applicable

Thanks for the reply, and sorry for the long response time.  I have been messing around with the api after looking at the example you linked me to, and I see how the example shows that you can alter the Curve Element's current LineStyle, but my end goal here is to select a curve that has a line style of say pen 01, and change it to linestyle pen 02.  Is this possible?  Or is it only capable of altering the settings of the current line style?

 

Below I will post the script I am currently running through.  Please take it with a grain of salt, as I am mostly copy and pasting snippets of other scripts I have written and is not entirely cleaned up or polished.  Also, I keep a task dialog at the end for auditing purposes.

 

 

        public void selectLine()
        {
            Document doc = this.ActiveUIDocument.Document;
            string parameterTypeError = ""
            string info = "";
            
            Autodesk.Revit.UI.Selection.Selection sel = this.ActiveUIDocument.Selection;
            Reference Selln = sel.PickObject(ObjectType.Element, "please pick an import instance");
            
            
            ImportInstance dwg = doc.GetElement(Selln) as ImportInstance;

            Element e = doc.GetElement(Selln);
            
            GeometryObject geomObj = e.GetGeometryObjectFromReference(Selln);
            
                    
            TaskDialog.Show("Element Info", e.Name + Environment.NewLine + e.Id + Environment.NewLine);
            
            {
            using(Transaction t = new Transaction(doc, "Set annotation parameters"))
            {
                t.Start();
                foreach (Element l in new FilteredElementCollector(doc)
                    .OfClass(typeof(CurveElement)))
                {
                    CurveElement crv = l as CurveElement;
                    
                    GraphicsStyle gs = crv.LineStyle as GraphicsStyle;
                
                    //This is where things start to fall apart for me and I'm not sure what to alter
                    gs.GraphicsStyleCategory;
                
                }
                //This was inlcuded incase changing the style required altering a parameter
                if (parameterTypeError != "")
                {
                    TaskDialog.Show("Error","Parameter must be a string.\n" + parameterTypeError);
                    t.RollBack();
                }
                else
                    t.Commit();                            
            }
            
            TaskDialog.Show("elements",info);
        }
        }
    }

0 Likes
Message 4 of 8

Anonymous
Not applicable

Jeremy,

 

Thanks for your help so far.  I wanted to show you and anyone else interested an update on my progress.  I went through the example that you pointed me to and it was a lot of help, I found that the easiest way to change the linestyle of the curve element was by setting it equal to the linestyle of another curve, or to find the element id of the graphic style you want to change it to.  Since the element id of the graphic style proved difficult for me to collect, i instead opted to search for curves with the line styles I wanted.  Eventually I hope to create a line styles sheet that would store an example line of each style I wish to store and then cast to prevent the script from over searching, and also offer teams a visual guide to the line styles we want to limit our projects to.  Below is my code, and I also included the file I set up to run it.  Please let me know if you have any ideas on how to make the code clearner.

 

Thanks,

 

		public void replaceLineStyles()
		{
			Document doc = this.ActiveUIDocument.Document;
			//string parameterTypeError = ""; 
			//string info = "";
			
			//Declare the Graphic styles that you wish to store the values of
			GraphicsStyle red = null;
			GraphicsStyle yellow = null;
			GraphicsStyle green = null;
			GraphicsStyle blue = null;
			
			//Collect the lines in the Model
			foreach (Element e in new FilteredElementCollector(doc)
			         .OfClass(typeof(CurveElement)))
			{
				//Cast the element as a curve element
				CurveElement ln = e as CurveElement;
				
				//Begin transaction
				using(Transaction t = new Transaction(doc, "Change Line Styles"))
					{
						t.Start();
						
						
						//Begin to look for the line styles you wish to capture 
						//
						//	(in future iterations I will place a sheet with examples of each line style I want to capture
						//	in my file, and search through the lines on the sheet for the styles I want to capture 
						//	rather than the entire document)
						if (ln.LineStyle.Name.Contains("Red"))
						{
							red = ln.LineStyle as GraphicsStyle;
						}
						
						if (ln.LineStyle.Name.Contains("Yellow"))
						{
							yellow = ln.LineStyle as GraphicsStyle;
						}
						
						if (ln.LineStyle.Name.Contains("Green"))
						{
							green = ln.LineStyle as GraphicsStyle;
						}
						
						if (ln.LineStyle.Name.Contains("Blue"))
						{
							blue = ln.LineStyle as GraphicsStyle;
						}				
						
						//Begin to look for the line styles you wish to replace and replace them
						if (ln.LineStyle.Name.Contains("Purple"))
						{
							ln.LineStyle = red;
						}				
						
						if (ln.LineStyle.Name.Contains("Pink"))
						{
							ln.LineStyle = yellow;
						}				
						
						if (ln.LineStyle.Name.Contains("Cyan"))
						{
							ln.LineStyle = green;
						}
						
						if (ln.LineStyle.Name.Contains("Orange"))
						{
							ln.LineStyle = blue;
						}
						
						t.Commit();
					}
				}
			}

 

0 Likes
Message 5 of 8

jeremytammik
Autodesk
Autodesk

Looks fine to me.

 

Please write a really short description of the whole utility you envision to better understand the whole thing.

 

For example: collect all the graphic styles to choose from (how to identify them?), pick an element, assign it one of the styles.

 

Is that it?

 

Cheers

 

Jeremy



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

0 Likes
Message 6 of 8

codecavepro
Explorer
Explorer

As always this will work only on English versions of Revit, because GraphicsStyle.Name is localized.
Do you know how to select certain GraphicsStyle entries in a language-agnostic way?
E.g. I would like to select 3 styles "thin lines", "thick lines" and "medium lines"

0 Likes
Message 7 of 8

jeremytammik
Autodesk
Autodesk

Those three are presumably built into Revit, more or less.

 

If all your designs originate from the same template file, you could use the line style element ids to identify them.

 

Alternatively, you might be able to use RevitLookup and other, more intimate database exploration tools to find some criteria to identify them.

 



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

0 Likes
Message 8 of 8

Kevin_Zurro
Participant
Participant

Dear @Anonymous. I have this code that creates a "DetailCurve" and assigns a line style (Image 1) based on a "DataGridView" (Image 2).

 

Steps:

 

1. Checks the diameter of the bar and looks for it in the first column of the DataGridView, gets the match, and returns the "Category" found in the second column.

 

2. Draw the "DetailCurve" based on a view and an "IList <Curve>".

 

3. With that "Category" create the "GraphicsStyle" with a "GraphicsStyleType.Projection".

 

4. Finally, assign that "GraphicsStyle" to the "DetailCurve" created previously.

 

        ///<summary> Cambia el estilo de la línea en función del DataGridView </summary>
        public static List<Element> DibujarArmaduraSegunDatagridview(System.Windows.Forms.DataGridView dgv, Document doc, Rebar barra, View vista)
        {
            // Obtiene el diámetro de la barra
            RebarBarType barraDiametro = doc.GetElement(barra.GetTypeId()) as RebarBarType;

            // Crea la lista de las lineas a devolver
            List<Element> listaLineas = new List<Element>();

            // Crea la lista de los estilos de lineas del proyecto
            List<Category> estilos = Tools.ObtenerEstilosDeLinea(doc);

            // Variable necesarias
            Category cat = null;

            // Ajustar las curvas si se superponen
            bool superposicion = false;

            // Omitir ganchos
            bool ganchos = false;

            // Omitir los diámetros de doblez
            bool radioDeGiro = false;

            // Define si es Multi-Planar o no
            MultiplanarOption multiplePlano = MultiplanarOption.IncludeOnlyPlanarCurves;

            // Selecciona cual barra del conjunto dibuja
            int posicion = 0; //barra.NumberOfBarPositions - 1;

            // Obtiene una lista con curvas de la linea central de la barra
            IList<Curve> listaCurva = barra.GetCenterlineCurves(superposicion, ganchos, radioDeGiro, multiplePlano, posicion);

            try
            {
                // Recorre todas las curvas de la lista
                foreach (Curve curva in listaCurva)
                {
                    // Recorre el DataGridView
                    for (int i = 0; i < dgv.Rows.Count; i++)
                    {
                        // Compara el nombre de la celda con el diámetro de la barra
                        if (dgv.Rows[i].Cells[0].Value.ToString() == barraDiametro.Name)
                        {
                            // Crea una serie de curvas con lineas de detalle
                            DetailCurve curvaDetalle = doc.Create.NewDetailCurve(vista, curva);

                            // Obtiene la categoría del estilo de línea
                            cat = estilos.FirstOrDefault(x => x.Name == dgv.Rows[i].Cells[1].Value.ToString());

                            // Crea el estilo de gráfico en función del estilo de línea
                            GraphicsStyle gra = cat.GetGraphicsStyle(GraphicsStyleType.Projection);
                                                        
                            // Asigna el estilo a la línea
                            curvaDetalle.LineStyle = gra;

                            // Agrega la linea a la lista de lineas
                            listaLineas.Add(curvaDetalle);
                        }
                    }

                }
            }
            catch (Exception) { }

            return listaLineas;
        }

 

I hope it helps you, greetings.

0 Likes