Change Viewport type

Change Viewport type

will.wydock
Advocate Advocate
5,419 Views
14 Replies
Message 1 of 15

Change Viewport type

will.wydock
Advocate
Advocate
I am working on a routine that would be used to change types on Viewports. How can I change family types for the VP based on the name string value rather than the Id?
0 Likes
Accepted solutions (1)
5,420 Views
14 Replies
Replies (14)
Message 2 of 15

jeremytammik
Autodesk
Autodesk

Dear Wwydock,

 

Thank you for your query.

 

Unfortunately, it leaves me still wondering what is is you wish to achieve.

 

Can you clarify, please?

 

Thank you!

 

Best regards,

 

Jeremy



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

0 Likes
Message 3 of 15

will.wydock
Advocate
Advocate

What i am trying to achieve is my company's standard is to place a graphic scale at the far end of the view title so what have done is created a viewtitle (at various lengths for word wrapping. What i want to do is when i run my routine i will select a viewport and it will read the current viewport type(6" Title ) and replace it will with the type that has graphic scale appended to  it(6" Title w/Scale) and then it would fill in the graphic scale values based on what it pulls from the "Scalevalue 1:"

 

I am still working on applying the values to the viewtitle graphic scale but here is what i have

 

 

		public void GraphicScale()
		{
			
			UIDocument uidoc=this.ActiveUIDocument;
			Document doc= uidoc.Document;
			
			ISelectionFilter viewportfilter = new ViewPortPickFilter();
            Reference refa = uidoc.Selection.PickObject(ObjectType.Element, viewportfilter, "Select a viewport.");
            Element e = doc.GetElement(refa);
            //get view associated with viewport, 
            FilteredElementCollector viewCollector = new FilteredElementCollector(doc).OfClass(typeof(View)).
			
            using (Transaction trans = new Transaction(doc, "Rename Sheets"))
            {
                trans.Start();
                {

                    Parameter paramscalefactor = e.LookupParameter("Scale Value    1:");
                    double vscalefactor = Convert.ToDouble(paramscalefactor.AsValueString());
					
                    e.LookupParameter("SCALE1").Set(vscalefactor/24);
                    e.LookupParameter("SCALE2").Set(vscalefactor/12);
                    e.LookupParameter("SCALE3").Set(vscalefactor/8);
                    e.LookupParameter("SCALE4").Set(vscalefactor/6);
                    //TaskDialog.Show("Test","Graphic Scale is "+vscalefactor);

                }
                trans.Commit();
            }
        }
    
	}
	 public class ViewPortPickFilter : ISelectionFilter
    {
        public bool AllowElement(Element e)
        {
            return (e.Category.Id.IntegerValue.Equals(
             (int)BuiltInCategory.OST_Viewports));
        }

        public bool AllowReference(Reference r, XYZ p)
        {
            return false;
        }
    }
}

 

 

 

0 Likes
Message 4 of 15

RPTHOMAS108
Mentor
Mentor

Not sure why you would want to do this? From your description it sounds like this can be done via editing the family of the annotation symbol used for the viewport title:

 

24-02-1700.png

 

As far as your code goes I recommend using BIP values rather than looking up by parameter name

VIEW_SCALE_PULLDOWN_METRIC -1005151

or

VIEW_SCALE_PULLDOWN_IMPERIAL -1005152

 

Both return an integer so no need to incur the overhead of the .AsValueString method coupled with converting string to a double, just use .AsInteger

 

0 Likes
Message 5 of 15

will.wydock
Advocate
Advocate

This is the graphic scale that i am referring to, I have already created the necessary family types with the lengths.  Our standard is to have the default viewport type not include the graphic scale. So I woulds like users to run a routine that would automatically switch over the type to one that includes graphic scale and fill in the values for the scale.

Capture.PNG

 

0 Likes
Message 6 of 15

RPTHOMAS108
Mentor
Mentor

I understand what you mean now.

 

Your scale parameters need to be shared parameters within the annotation symbol family that is used for the viewport title. Then you bind the same shared parameters to the views category in the project. You’ll then be able to set the parameter values on the associated view and they’ll get passed through to the viewport title block family and appear on the sheet.

 

I believe that is the only way.

0 Likes
Message 7 of 15

FAIR59
Advisor
Advisor
Accepted solution

to find and replace the viewporttype use  this code:

 

			const char char_quote = (char) 34;
			string viewport_with_scale_Name = string.Format("6{0} Title w/Scale",char_quote); //6" Title w/Scale
			 		
			ElementType scaledViewPortType = null;
			ElementType currentViewprtType = doc.GetElement(viewport.GetTypeId()) as ElementType;
 			if (currentViewprtType.Name != viewport_with_scale_Name)
 			{
 				foreach (ElementId id in viewport.GetValidTypes())
 				{
 					ElementType typ = doc.GetElement(id) as ElementType;
 					if (typ.Name == viewport_with_scale_Name)
 					{
 						scaledViewPortType = typ;
 						break;
 					}
 				}
 			}
 			if (scaledViewPortType!= null)
 			{
 				using (Transaction t = new Transaction(doc,"change_viewporttype"))
 				{
 					t.Start();
 					viewport.ChangeTypeId(scaledViewPortType.Id);
 					doc.Regenerate();
 					t.Commit();
 				}
 			}
Message 8 of 15

will.wydock
Advocate
Advocate

Thank you,

 

I was looking at what you had posted and would like to utilize it, but I am having trouble with either setting up different if statements or concatenating on to the existing viewport name. When I am trying to test it in a dialog box I am getting an error that it is an empty value

 

this is what i was using

 

string vpname="";			
vpname=currentViewprtType.Name;
TaskDialog.Show("",vpname);

is there something i am missing or another way to accomplish this?

 

Thanks,

0 Likes
Message 9 of 15

jeremytammik
Autodesk
Autodesk

Dear Wwydock,

 

Thank you for your update and sorry to hear you are still struggling with this.

 

Have you tried running your code in the debugger and stepping through it line by line?

 

That should make any such problem pretty apparent and easy to identify.

 

Cheers,

 

Jeremy



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

0 Likes
Message 10 of 15

RPTHOMAS108
Mentor
Mentor

TaskDialog.Show("",vpname);

 

TaskDialog can't have empty title.

0 Likes
Message 11 of 15

will.wydock
Advocate
Advocate

Thanks, that was a dumb mistake on my part.

 

As part of my routine i changed the viewport types and applied parameter values based on scale. Everything changes except the changes in the parameter values. Is there a way to graphically refresh the element? I have already tried doc.regenerate and uidoc.refreshactiveview.

 

Thanks

0 Likes
Message 12 of 15

jeremytammik
Autodesk
Autodesk
0 Likes
Message 13 of 15

RPTHOMAS108
Mentor
Mentor

Have you created shared parameters and linked them through in the manor as noted above?

0 Likes
Message 14 of 15

will.wydock
Advocate
Advocate

I was able to get it to work by creating a second transaction and using 

  ElementTransformUtils.MoveElement(doc, viewport.Id, XYZ.BasisX);
  ElementTransformUtils.MoveElement(doc, viewport.Id, XYZ.BasisX.Negate());
0 Likes
Message 15 of 15

jeremytammik
Autodesk
Autodesk

Dear Wwydock,

 

Congratulations on fixing it, and thank you for letting us know.

 

I am glad that the move-back-and-forth trick helps.

 

Did you try it with a zero vector as well?

 

Did you try any of the other methods?

 

Cheers,

 

Jeremy



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

0 Likes