From 4e0ce5031fa1dc0cac02ea6c5424d4df84ed5cbf Mon Sep 17 00:00:00 2001 From: Jeremy Tammik Date: Mon, 18 May 2015 17:47:16 +0200 Subject: [PATCH] second attempt at integrating version compatibility extension methods by Magson Leone --- .../BuildingCoder/Properties/AssemblyInfo.cs | 5 +- BuildingCoder/BuildingCoder/Util.cs | 1159 +++++++++++------ 2 files changed, 746 insertions(+), 418 deletions(-) diff --git a/BuildingCoder/BuildingCoder/Properties/AssemblyInfo.cs b/BuildingCoder/BuildingCoder/Properties/AssemblyInfo.cs index 04fdcdce..6e4b35ff 100644 --- a/BuildingCoder/BuildingCoder/Properties/AssemblyInfo.cs +++ b/BuildingCoder/BuildingCoder/Properties/AssemblyInfo.cs @@ -91,6 +91,7 @@ // 2015-04-13 2015.0.120.9 implemented Plane Compare method // 2015-05-05 2015.0.120.10 cleaned up, tested and fixed a bug in CmdCopyWallType // 2015-05-15 2015.0.120.11 started integrating version compatibility extension methods by Magson Leone +// 2015-05-18 2015.0.120.12 second attempt at integrating version compatibility extension methods by Magson Leone // -[assembly: AssemblyVersion( "2015.0.120.11" )] -[assembly: AssemblyFileVersion( "2015.0.120.11" )] +[assembly: AssemblyVersion( "2015.0.120.12" )] +[assembly: AssemblyFileVersion( "2015.0.120.12" )] diff --git a/BuildingCoder/BuildingCoder/Util.cs b/BuildingCoder/BuildingCoder/Util.cs index f91f7ede..5b61d31c 100644 --- a/BuildingCoder/BuildingCoder/Util.cs +++ b/BuildingCoder/BuildingCoder/Util.cs @@ -1253,563 +1253,890 @@ public static IEnumerable OfType( } #endregion // Extension Method Classes - #region Compatibilty Methods by Magson Leone - public static class CompatibiltyMethods + #region Compatibility Methods by Magson Leone + public static class CompatibilityMethods { - public static Element PegarElemento( this Document doc, ElementId id ) + #region Curve + public static XYZ GetPoint2( + this Curve curva, + int i ) { - Element ele = null; - //ele = doc.get_Element(id); - //ele = doc.GetElement(id); - MethodInfo met = doc.GetType().GetMethod( "get_Element", new Type[] { typeof( ElementId ) } ); - if( met == null ) - met = doc.GetType().GetMethod( "GetElement", new Type[] { typeof( ElementId ) } ); - ele = met.Invoke( doc, new object[] { id } ) as Element; - return ele; - } - public static Element PegarElemento( this Document doc, Reference refe ) - { - Element ele = null; - ele = doc.GetElement( refe ); - return ele; - } - public static char PegarDecimalSymbol( this Document doc ) - { - char valor = ','; - MethodInfo met = doc.GetType().GetMethod( "GetUnits" ); - if( met != null ) + XYZ value = null; + try { - object temp = met.Invoke( doc, null ); - PropertyInfo prop = temp.GetType().GetProperty( "DecimalSymbol" ); - object o = prop.GetValue( temp, null ); - if( o.ToString() == "Comma" ) - valor = ','; - else - valor = '.'; + MethodInfo met = curva.GetType() + .GetMethod( "GetEndPoint", new Type[] { typeof( int ) } ); + if( met == null ) + met = curva.GetType() + .GetMethod( "get_EndPoint", new Type[] { typeof( int ) } ); + value = met.Invoke( curva, + new object[] { i } ) as XYZ; } - else + // Catching all exceptions is not a good idea: + // http://stackoverflow.com/questions/315948/c-catching-all-exceptions + // http://blogs.msdn.com/b/dotnet/archive/2009/02/19/why-catch-exception-empty-catch-is-bad.aspx + catch { } + return value; + } + #endregion // Curve + + #region definitions + public static Definition Create2( this + Definitions definitions, Document doc, string nome, + ParameterType tipo, bool visibilidade ) + { + Definition value = null; + try { - object temp = doc.GetType().GetProperty( "ProjectUnit" ); - PropertyInfo prop = temp.GetType().GetProperty( "DecimalSymbolType" ); - object o = prop.GetValue( temp, null ); - if( o.ToString() == "DST_COMMA" ) - valor = ','; + List ls = doc.GetType().Assembly + .GetTypes().Where( a => a.IsClass && a + .Name == "ExternalDefinitonCreationOptions" ).ToList(); + if( ls.Count > 0 ) + { + Type t = ls[0]; + ConstructorInfo c = t + .GetConstructor( new Type[] { typeof(string), + typeof(ParameterType) } ); + object ed = c + .Invoke( new object[] { nome, tipo } ); + ed.GetType().GetProperty( "Visible" ) + .SetValue( ed, visibilidade, null ); + value = definitions.GetType() + .GetMethod( "Create", new Type[] { t } ).Invoke( definitions, + new object[] { ed } ) as Definition; + } else - valor = '.'; + { + value = definitions.GetType() + .GetMethod( "Create", new Type[] { typeof(string), + typeof(ParameterType), typeof(bool) } ).Invoke( definitions, + new object[] { nome, tipo, + visibilidade } ) as Definition; + } } - return valor; + catch { } + return value; } - public static Element Nivel_( this Element ele ) - { - Document doc = ele.Document; - Element e = null; - //e = ele.Level; - //e = PegarElemento(doc, ele.LevelId); - Type t = ele.GetType(); - if( t.GetProperty( "Level" ) != null ) - e = t.GetProperty( "Level" ).GetValue( ele, null ) as Element; - else - e = doc.PegarElemento( (ElementId) t.GetProperty( "LevelId" ).GetValue( ele, null ) ); - return e; + #endregion // definitions - } - public static List Materiais_( this Element ele ) + #region document + public static Element GetElement2( this Document + doc, ElementId id ) { - List mats = new List(); - //mats = ele.Materials.Cast().ToList(); - //mats = ele.GetMaterialIds(false).Select(a => PegarElemento(doc, a)).Cast().ToList(); - Type t = ele.GetType(); - if( t.GetProperty( "Materials" ) != null ) - mats = ( (IEnumerable) t.GetProperty( "Materials" ).GetValue( ele, null ) ).Cast().ToList(); - else + Element value = null; + try { - Document doc = ele.Document; - MethodInfo met = t.GetMethod( "GetMaterialIds", new Type[] { typeof( bool ) } ); - mats = ( (ICollection) met.Invoke( ele, new object[] { false } ) ).Select( a => PegarElemento( doc, a ) ).Cast().ToList(); + MethodInfo met = doc.GetType() + .GetMethod( "get_Element", new Type[] { typeof( ElementId ) } ); + if( met == null ) + met = doc.GetType() + .GetMethod( "GetElement", new Type[] { typeof( ElementId ) } ); + value = met.Invoke( doc, + new object[] { id } ) as Element; } - return mats; + catch { } + return value; } -#if _THESE_METHODS_CAUSE_COMPILER_ERRORS - public static List Elementos( this Autodesk.Revit.UI.Selection.Selection sel ) + public static Element GetElement2( this Document + doc, Reference refe ) { - List list_elements = new List(); - //list_elements = sel.Elements.Cast().ToList(); - //list_elements = sel.PegarElementoIds().ToList().Select(a => PegarElemento(doc, a)).ToList(); - Type t = sel.GetType(); - if( t.GetProperty( "PegarElementoIds" ) != null ) - { - MethodInfo met = t.GetMethod( "PegarElementoIds" ); - list_elements = ( (ICollection) met.Invoke( sel, null ) ).Select( a => PegarElemento( doc, a ) ).ToList(); - - } - else + Element value = null; + try { - list_elements = ( (SelElementSet) t.GetProperty( "Elements" ).GetValue( sel, null ) ).Cast().ToList(); - + value = doc.GetElement( refe ); } - return list_elements; + catch { } + return value; } - //public static void Clear(this Autodesk.Revit.UI.Selection.Selection sel) - //{ - // sel.Elements.Clear(); - - - - //} - public static Parameter PegarParametro_( this Element ele, string nome_paramentro ) + public static Line CreateLine2( this Document doc, + XYZ p1, XYZ p2, bool bound = true ) { - Parameter pa = null; - //pa = ele.get_Parameter(nome_paramentro); - //pa = ele.LookupParameter(nome_paramentro); - Type t = ele.GetType(); - MethodInfo met = t.GetMethod( "LookupParameter", new Type[] { typeof( string ) } ); - if( met == null ) - met = t.GetMethod( "get_Parameter", new Type[] { typeof( string ) } ); - pa = met.Invoke( ele, new object[] { nome_paramentro } ) as Parameter; - return pa; - } - public static Parameter PegarParametro_( this Element ele, BuiltInParameter builtInParameter ) - { - Parameter pa = null; - //pa = ele.get_Parameter(builtInParameter); - //pa = ele.LookupParameter(builtInParameter); - Type t = ele.GetType(); - MethodInfo met = t.GetMethod( "LookupParameter", new Type[] { typeof( BuiltInParameter ) } ); - if( met == null ) - met = t.GetMethod( "get_Parameter", new Type[] { typeof( BuiltInParameter ) } ); - pa = met.Invoke( ele, new object[] { builtInParameter } ) as Parameter; - return pa; - } - public static Definition Criar( this Definitions definitions, string nome, ParameterType tipo, bool visibilidade ) - { - Definition def = null; - //def = definitions.Create(nome, tipo, visibilidade); - //ExternalDefinitonCreationOptions ed = new ExternalDefinitonCreationOptions(nome, tipo); - //ed.Visible = visibilidade; - //def = definitions.Create(ed); - List ls = doc.GetType().Assembly.GetTypes().Where( a => a.IsClass && a.Name == "ExternalDefinitonCreationOptions" ).ToList(); - - if( ls.Count > 0 ) + Line value = null; + try { - Type t = ls[0]; - - ConstructorInfo c = t.GetConstructor( new Type[] { typeof( string ), typeof( ParameterType ) } ); - - object ed = c.Invoke( new object[] { nome, tipo } ); - - ed.GetType().GetProperty( "Visible" ).SetValue( ed, visibilidade, null ); - - def = definitions.GetType().GetMethod( "Create", new Type[] { t } ).Invoke( definitions, new object[] { ed } ) as Definition; + object[] parametros = new object[] { p1, + p2 }; + Type[] tipos = parametros.Select( a => a + .GetType() ).ToArray(); + string metodo = "CreateBound"; + if( bound == false ) metodo = + "CreateUnbound"; + MethodInfo met = typeof( Line ) + .GetMethod( metodo, tipos ); + if( met != null ) + { + value = met.Invoke( null, + parametros ) as Line; + } + else + { + parametros = new object[] { p1, p2, + bound }; + tipos = parametros.Select( a => a + .GetType() ).ToArray(); + value = doc.Application.Create + .GetType().GetMethod( "NewLine", tipos ).Invoke( doc + .Application.Create, parametros ) as Line; + } } - else + catch { } + return value; + } + public static Wall CreateWall2( this Document doc, + Curve curve, ElementId wallTypeId, + ElementId levelId, double height, double offset, bool flip, + bool structural ) + { + Wall value = null; + try { - def = definitions.GetType().GetMethod( "Create", new Type[] { typeof( string ), typeof( ParameterType ), typeof( bool ) } ).Invoke( definitions, new object[] { nome, tipo, visibilidade } ) as Definition; + object[] parametros = new object[] { doc, + curve, wallTypeId, levelId, height, offset, flip, + structural }; + Type[] tipos = parametros.Select( a => a + .GetType() ).ToArray(); + MethodInfo met = typeof( Wall ) + .GetMethod( "Create", tipos ); + if( met != null ) + { + value = met.Invoke( null, + parametros ) as Wall; + } + else + { + parametros = new object[] { curve, + (WallType)doc.GetElement2(wallTypeId), (Level)doc + .GetElement2(levelId), height, offset, flip, + structural }; + tipos = parametros.Select( a => a + .GetType() ).ToArray(); + value = doc.Create.GetType() + .GetMethod( "NewWall", tipos ).Invoke( doc.Create, + parametros ) as Wall; + } } - - return def; + catch { } + return value; } - public static double PegarMaterialArea_( this Element ele, Material m ) + public static Arc CreateArc2( this Document doc, + XYZ p1, XYZ p2, XYZ p3 ) { - double valor = 0; - //valor = ele.GetMaterialArea(m); - //valor = ele.GetMaterialArea(m.Id, false); - Type t = ele.GetType(); - MethodInfo met = t.GetMethod( "GetMaterialArea", new Type[] { typeof( ElementId ), typeof( bool ) } ); - if( met != null ) + Arc value = null; + try { - valor = (double) met.Invoke( ele, new object[] { m.Id, false } ); + object[] parametros = new object[] { p1, + p2, p3 }; + Type[] tipos = parametros.Select( a => a + .GetType() ).ToArray(); + string metodo = "Create"; + MethodInfo met = typeof( Arc ) + .GetMethod( metodo, tipos ); + if( met != null ) + { + value = met.Invoke( null, + parametros ) as Arc; + } + else + { + value = doc.Application.Create + .GetType().GetMethod( "NewArc", tipos ).Invoke( doc + .Application.Create, parametros ) as Arc; + } } - else + catch { } + return value; + } + public static char GetDecimalSymbol2( this + Document doc ) + { + char valor = ','; + try { - met = t.GetMethod( "GetMaterialArea", new Type[] { typeof( Element ) } ); - valor = (double) met.Invoke( ele, new object[] { m } ); + MethodInfo met = doc.GetType() + .GetMethod( "GetUnits" ); + if( met != null ) + { + object temp = met.Invoke( doc, null ); + PropertyInfo prop = temp.GetType() + .GetProperty( "DecimalSymbol" ); + object o = prop.GetValue( temp, null ); + if( o.ToString() == "Comma" ) + valor = ','; + else + valor = '.'; + } + else + { + object temp = doc.GetType() + .GetProperty( "ProjectUnit" ).GetValue( doc, null ); + PropertyInfo prop = temp.GetType() + .GetProperty( "DecimalSymbolType" ); + object o = prop.GetValue( temp, null ); + if( o.ToString() == "DST_COMMA" ) + valor = ','; + else + valor = '.'; + } } + catch { } return valor; } - public static XYZ PegarPonto( this Curve curva, int i ) - { - XYZ p = null; - //p = curva.GetEndPoint(i); - //p = curva.PegarPonto(i); - MethodInfo met = curva.GetType().GetMethod( "GetEndPoint", new Type[] { typeof( int ) } ); - if( met == null ) - met = curva.GetType().GetMethod( "get_EndPoint", new Type[] { typeof( int ) } ); - p = met.Invoke( curva, new object[] { i } ) as XYZ; - return p; - } - public static Wall CriarParede( this Document document, Curve curve, ElementId wallTypeId, ElementId levelId, double height, double offset, bool flip, bool structural ) + public static void UnjoinGeometry2( this Document + doc, Element firstElement, Element secondElement ) { - Wall w = null; - //w = g.doc.Create.NewWall(curve, (WallType)g.doc.PegarElemento(wallTypeId), (Level)g.doc.PegarElemento(levelId), height, offset, flip, structural); - //w = Wall.Create(document, curve, wallTypeId, levelId, height, offset, flip, structural); - - - object[] parametros = new object[] { document, curve, wallTypeId, levelId, height, offset, flip, structural }; - Type[] tipos = parametros.Select( a => a.GetType() ).ToArray(); - - MethodInfo met = typeof( Wall ).GetMethod( "Create", tipos ); - - if( met != null ) - { - w = met.Invoke( null, parametros ) as Wall; - } - else + try { - parametros = new object[] { curve, (WallType) doc.PegarElemento( wallTypeId ), (Level) doc.PegarElemento( levelId ), height, offset, flip, structural }; - tipos = parametros.Select( a => a.GetType() ).ToArray(); - - w = doc.Create.GetType().GetMethod( "NewWall", tipos ).Invoke( doc.Create, parametros ) as Wall; + List ls = doc.GetType().Assembly + .GetTypes().Where( a => a.IsClass && a + .Name == "JoinGeometryUtils" ).ToList(); + object[] parametros = new object[] { doc, + firstElement, secondElement }; + Type[] tipos = parametros.Select( a => a + .GetType() ).ToArray(); + if( ls.Count > 0 ) + { + Type t = ls[0]; + MethodInfo met = t + .GetMethod( "UnjoinGeometry", tipos ); + met.Invoke( null, parametros ); + } } - - return w; + catch { } } - public static Line CriarLinha( this Document document, XYZ p1, XYZ p2, bool bound = true ) + public static void JoinGeometry2( this Document + doc, Element firstElement, Element secondElement ) { - Line l = null; - //l = Line.CreateBound(p1, p2); - //l = g.doc.Application.Create.NewLine(p1, p2, true); - - object[] parametros = new object[] { p1, p2 }; - Type[] tipos = parametros.Select( a => a.GetType() ).ToArray(); - string metodo = "CreateBound"; - if( bound == false ) metodo = "CreateUnbound"; - MethodInfo met = typeof( Line ).GetMethod( metodo, tipos ); - if( met != null ) + try { - l = met.Invoke( null, parametros ) as Line; + List ls = doc.GetType().Assembly + .GetTypes().Where( a => a.IsClass && a + .Name == "JoinGeometryUtils" ).ToList(); + object[] parametros = new object[] { doc, + firstElement, secondElement }; + Type[] tipos = parametros.Select( a => a + .GetType() ).ToArray(); + if( ls.Count > 0 ) + { + Type t = ls[0]; + MethodInfo met = t + .GetMethod( "JoinGeometry", tipos ); + met.Invoke( null, parametros ); + } } - else + catch { } + } + public static bool IsJoined2( this Document doc, + Element firstElement, Element secondElement ) + { + bool value = false; + try { - parametros = new object[] { p1, p2, bound }; - tipos = parametros.Select( a => a.GetType() ).ToArray(); - l = doc.Application.Create.GetType().GetMethod( "NewLine", tipos ).Invoke( doc.Application.Create, parametros ) as Line; + List ls = doc.GetType().Assembly + .GetTypes().Where( a => a.IsClass && a + .Name == "JoinGeometryUtils" ).ToList(); + object[] parametros = new object[] { doc, + firstElement, secondElement }; + Type[] tipos = parametros.Select( a => a + .GetType() ).ToArray(); + if( ls.Count > 0 ) + { + Type t = ls[0]; + MethodInfo met = t + .GetMethod( "AreElementsJoined", tipos ); + value = (bool) met.Invoke( null, + parametros ); + } } - return l; - } - public static Arc CriarArco( this Document document, XYZ p1, XYZ p2, XYZ p3 ) - { - Arc arc = null; - //arc = g.doc.Application.Create.NewArc(p1, p2, p3); - //arc = Arc.Create(p1, p2, p3); - object[] parametros = new object[] { p1, p2, p3 }; - Type[] tipos = parametros.Select( a => a.GetType() ).ToArray(); - string metodo = "Create"; - MethodInfo met = typeof( Arc ).GetMethod( metodo, tipos ); - if( met != null ) + catch { } + return value; + } + public static bool CalculateVolumeArea2( this + Document doc, bool value ) + { + try { - arc = met.Invoke( null, parametros ) as Arc; + List ls = doc.GetType().Assembly + .GetTypes().Where( a => a.IsClass && a + .Name == "AreaVolumeSettings" ).ToList(); + if( ls.Count > 0 ) + { + Type t = ls[0]; + object[] parametros = new object[] { + doc }; + Type[] tipos = parametros + .Select( a => a.GetType() ).ToArray(); + MethodInfo met = t + .GetMethod( "GetAreaVolumeSettings", tipos ); + object temp = met.Invoke( null, + parametros ); + temp.GetType() + .GetProperty( "ComputeVolumes" ).SetValue( temp, value, null ); + } + else + { + PropertyInfo prop = doc.Settings + .GetType().GetProperty( "VolumeCalculationSetting" ); + object temp = prop.GetValue( doc + .Settings, null ); + prop = temp.GetType() + .GetProperty( "VolumeCalculationOptions" ); + temp = prop.GetValue( temp, null ); + prop = temp.GetType() + .GetProperty( "VolumeComputationEnable" ); + prop.SetValue( temp, value, null ); + } } - else + catch { } + return value; + } + public static Group CreateGroup2( this Document + doc, List elementos ) + { + Group value = null; + try { - arc = doc.Application.Create.GetType().GetMethod( "NewArc", tipos ).Invoke( doc.Application.Create, parametros ) as Arc; + ElementSet eleset = new ElementSet(); + foreach( Element ele in elementos ) + { + eleset.Insert( ele ); + } + ICollection col = elementos + .Select( a => a.Id ).ToList(); + object obj = doc.Create; + MethodInfo met = obj.GetType() + .GetMethod( "NewGroup", new Type[] { col.GetType() } ); + if( met != null ) + { + met.Invoke( obj, new object[] { col } ); + } + else + { + met = obj.GetType() + .GetMethod( "NewGroup", new Type[] { eleset.GetType() } ); + met.Invoke( obj, + new object[] { eleset } ); + } } - return arc; + catch { } + return value; } - public static void VirarParede( this Wall wall ) + public static void Delete2( this Document doc, + Element ele ) { - //wall.flip(); - //wall.Flip(); - string metodo = "Flip"; - MethodInfo met = typeof( Wall ).GetMethod( metodo ); - if( met != null ) + try { - met.Invoke( wall, null ); + object obj = doc; + MethodInfo met = obj.GetType() + .GetMethod( "Delete", new Type[] { typeof( Element ) } ); + if( met != null ) + { + met.Invoke( obj, new object[] { ele } ); + } + else + { + met = obj.GetType() + .GetMethod( "Delete", new Type[] { typeof( ElementId ) } ); + met.Invoke( obj, new object[] { ele + .Id } ); + } } - else + catch { } + } + #endregion // document + + #region element + public static Element Level2( this Element ele ) + { + Element value = null; + try { - metodo = "flip"; - met = typeof( Wall ).GetMethod( metodo ); - met.Invoke( wall, null ); + Document doc = ele.Document; + Type t = ele.GetType(); + if( t.GetProperty( "Level" ) != null ) + value = t.GetProperty( "Level" ) + .GetValue( ele, null ) as Element; + else + value = doc.GetElement2( (ElementId) t + .GetProperty( "LevelId" ).GetValue( ele, null ) ); } + catch { } + return value; } - public static void DesunirGeometria( this Document document, Element firstElement, Element secondElement ) + public static List Materiais2( this + Element ele ) { + List value = new List(); try { - //JoinGeometryUtils.UnjoinGeometry(document, firstElement, secondElement); - List ls = doc.GetType().Assembly.GetTypes().Where( a => a.IsClass && a.Name == "JoinGeometryUtils" ).ToList(); - object[] parametros = new object[] { document, firstElement, secondElement }; - Type[] tipos = parametros.Select( a => a.GetType() ).ToArray(); - if( ls.Count > 0 ) + Document doc = ele.Document; + Type t = ele.GetType(); + if( t.GetProperty( "Materials" ) != null ) + value = ( (IEnumerable) t + .GetProperty( "Materials" ).GetValue( ele, null ) ).Cast() + .ToList(); + else { - Type t = ls[0]; - MethodInfo met = t.GetMethod( "UnjoinGeometry", tipos ); - met.Invoke( null, parametros ); + MethodInfo met = t + .GetMethod( "GetMaterialIds", new Type[] { typeof( bool ) } ); + value = ( (ICollection) met + .Invoke( ele, new object[] { false } ) ) + .Select( a => doc.GetElement2( a ) ).Cast().ToList(); } } - catch - { - - } + catch { } + return value; } - public static void UnirGeometria( this Document document, Element firstElement, Element secondElement ) + public static Parameter GetParameter2( this + Element ele, string nome_paramentro ) { + Parameter value = null; try { - //JoinGeometryUtils.JoinGeometry(document, firstElement, secondElement); - List ls = doc.GetType().Assembly.GetTypes().Where( a => a.IsClass && a.Name == "JoinGeometryUtils" ).ToList(); - object[] parametros = new object[] { document, firstElement, secondElement }; - Type[] tipos = parametros.Select( a => a.GetType() ).ToArray(); - if( ls.Count > 0 ) + Type t = ele.GetType(); + MethodInfo met = t + .GetMethod( "LookupParameter", new Type[] { typeof( string ) } ); + if( met == null ) + met = t.GetMethod( "get_Parameter", + new Type[] { typeof( string ) } ); + value = met.Invoke( ele, + new object[] { nome_paramentro } ) as Parameter; + if( value == null ) { - Type t = ls[0]; - MethodInfo met = t.GetMethod( "JoinGeometry", tipos ); - met.Invoke( null, parametros ); + var pas = ele.Parameters + .Cast().ToList(); + if( pas.Exists( a => a.Definition + .Name.ToLower() == nome_paramentro.Trim().ToLower() ) ) + value = pas.First( a => a + .Definition.Name.ToLower() == nome_paramentro.Trim() + .ToLower() ); } } - catch + catch { } + return value; + } + public static Parameter GetParameter2( this + Element ele, BuiltInParameter builtInParameter ) + { + Parameter value = null; + try { + Type t = ele.GetType(); + MethodInfo met = t + .GetMethod( "LookupParameter", new Type[] { typeof( BuiltInParameter ) } ); + if( met == null ) + met = t.GetMethod( "get_Parameter", + new Type[] { typeof( BuiltInParameter ) } ); + value = met.Invoke( ele, + new object[] { builtInParameter } ) as Parameter; } + catch { } + return value; } - public static bool EstáUnido( this Document document, Element firstElement, Element secondElement ) + public static double GetMaterialArea2( this + Element ele, Material m ) { - bool b = false; - //b = JoinGeometryUtils.AreElementsJoined(document, firstElement, secondElement); - List ls = doc.GetType().Assembly.GetTypes().Where( a => a.IsClass && a.Name == "JoinGeometryUtils" ).ToList(); - object[] parametros = new object[] { document, firstElement, secondElement }; - Type[] tipos = parametros.Select( a => a.GetType() ).ToArray(); - if( ls.Count > 0 ) + double value = 0; + try { - Type t = ls[0]; - MethodInfo met = t.GetMethod( "AreElementsJoined", tipos ); - b = (bool) met.Invoke( null, parametros ); + Type t = ele.GetType(); + MethodInfo met = t + .GetMethod( "GetMaterialArea", new Type[] { typeof(ElementId), + typeof(bool) } ); + if( met != null ) + { + value = (double) met.Invoke( ele, + new object[] { m.Id, false } ); + } + else + { + met = t.GetMethod( "GetMaterialArea", + new Type[] { typeof( Element ) } ); + value = (double) met.Invoke( ele, + new object[] { m } ); + } } - return b; + catch { } + return value; } - public static ElementId PegarFonte( this Part part ) + public static double GetMaterialVolume2( this + Element ele, Material m ) { - ElementId id = null; - - //id = part.GetSourceElementIds().First().HostElementId; - //id = part.OriginalDividedElementId; - - PropertyInfo prop = part.GetType().GetProperty( "OriginalDividedElementId" ); - if( prop != null ) - id = prop.GetValue( part, null ) as ElementId; - else + double value = 0; + try { - MethodInfo met = part.GetType().GetMethod( "GetSourceElementIds" ); - object temp = met.Invoke( part, null ); - met = temp.GetType().GetMethod( "First" ); - temp = met.Invoke( temp, null ); - prop = temp.GetType().GetProperty( "HostElementId" ); - id = prop.GetValue( temp, null ) as ElementId; + Type t = ele.GetType(); + MethodInfo met = t + .GetMethod( "GetMaterialVolume", new Type[] { typeof(ElementId), + typeof(bool) } ); + if( met != null ) + { + value = (double) met.Invoke( ele, + new object[] { m.Id, false } ); + } + else + { + met = t + .GetMethod( "GetMaterialVolume", new Type[] { typeof( ElementId ) } ); + value = (double) met.Invoke( ele, + new object[] { m.Id } ); + } } - - - return id; + catch { } + return value; } - public static List PegarObjetosGeometricos( this Element ele ) + public static List + GetGeometricObjects2( this Element ele ) { - List list = new List(); - + List value = + new List(); try { - //geometryElement; - //geometryElement.Objects; Options op = new Options(); object obj = ele.get_Geometry( op ); - PropertyInfo prop = obj.GetType().GetProperty( "Objects" ); + PropertyInfo prop = obj.GetType() + .GetProperty( "Objects" ); if( prop != null ) { obj = prop.GetValue( obj, null ); IEnumerable arr = obj as IEnumerable; foreach( GeometryObject geo in arr ) { - list.Add( geo ); + value.Add( geo ); } } else { - IEnumerable geos = obj as IEnumerable; + IEnumerable geos = + obj as IEnumerable; foreach( var geo in geos ) { - list.Add( geo ); + value.Add( geo ); } } - - - //System.Windows.Forms.MessageBox.Show(list.Count.ToString()); } - catch - //(Exception ex) + catch { } + return value; + } + #endregion // element + + #region familysymbol + public static void EnableFamilySymbol2( this + FamilySymbol fsymbol ) + { + try { - //System.Windows.Forms.MessageBox.Show(ex.Message + ex.StackTrace); - //mbox.TryCatch(ex); + MethodInfo met = fsymbol.GetType() + .GetMethod( "Activate" ); + if( met != null ) + { + met.Invoke( fsymbol, null ); + } } - - return list; + catch { } } - public static bool CalcularVolumeDeArea( this Document doc, bool b ) - { - //doc.Settings.VolumeCalculationSetting.VolumeCalculationOptions.VolumeComputationEnable = b; - //AreaVolumeSettings.GetAreaVolumeSettings(doc).ComputeVolumes = b; + #endregion // familysymbol + #region internaldefiniton + public static void VaryGroup2( this + InternalDefinition def, Document doc ) + { try { - List ls = doc.GetType().Assembly.GetTypes().Where( a => a.IsClass && a.Name == "AreaVolumeSettings" ).ToList(); - if( ls.Count > 0 ) + object[] parametros = new object[] { doc, + true }; + Type[] tipos = parametros.Select( a => a + .GetType() ).ToArray(); + MethodInfo met = def.GetType() + .GetMethod( "SetAllowVaryBetweenGroups", tipos ); + if( met != null ) { - Type t = ls[0]; - - object[] parametros = new object[] { doc }; - Type[] tipos = parametros.Select( a => a.GetType() ).ToArray(); - MethodInfo met = t.GetMethod( "GetAreaVolumeSettings", tipos ); - object temp = met.Invoke( null, parametros ); - temp.GetType().GetProperty( "ComputeVolumes" ).SetValue( temp, b, null ); + met.Invoke( def, parametros ); } + } + catch { } + } + #endregion // internaldefiniton + + #region part + public static ElementId GetSource2( this Part part ) + { + ElementId value = null; + try + { + PropertyInfo prop = part.GetType() + .GetProperty( "OriginalDividedElementId" ); + if( prop != null ) + value = prop.GetValue( part, + null ) as ElementId; else { - PropertyInfo prop = doc.Settings.GetType().GetProperty( "VolumeCalculationSetting" ); - object temp = prop.GetValue( doc.Settings, null ); - prop = temp.GetType().GetProperty( "VolumeCalculationOptions" ); - temp = prop.GetValue( temp, null ); - prop = temp.GetType().GetProperty( "VolumeComputationEnable" ); - prop.SetValue( temp, b, null ); + MethodInfo met = part.GetType() + .GetMethod( "GetSourceElementIds" ); + object temp = met.Invoke( part, null ); + met = temp.GetType() + .GetMethod( "First" ); + temp = met.Invoke( temp, null ); + prop = temp.GetType() + .GetProperty( "HostElementId" ); + value = prop.GetValue( temp, + null ) as ElementId; } } - catch( Exception ex ) - { - - System.Windows.Forms.MessageBox.Show( ex.Message + ex.StackTrace ); - } - - return b; + catch { } + return value; } - public static void SetarSeleção( this Selection sel, ICollection elementos ) - { - //sel.Elements.Clear(); - - sel.LimparSeleção(); - - - //uidoc.Selection.Elements.Add(g.doc.PegarElemento(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as ElementId)); + #endregion // part - //SelElementSet aa; - //sel.SetElementIds(); - - object[] parametros = new object[] { elementos }; - Type[] tipos = parametros.Select( a => a.GetType() ).ToArray(); - - MethodInfo met = sel.GetType().GetMethod( "SetElementIds", tipos ); - - if( met != null ) + #region selection + public static List GetSelection2( this + Selection sel, Document doc ) + { + List value = new List(); + try { - met.Invoke( sel, parametros ); + sel.GetElementIds(); + Type t = sel.GetType(); + if( t.GetMethod( "GetElementIds" ) != null ) + { + MethodInfo met = t + .GetMethod( "GetElementIds" ); + value = ( (ICollection) met + .Invoke( sel, null ) ).Select( a => doc.GetElement2( a ) ) + .ToList(); + } + else + { + value = ( (IEnumerable) t + .GetProperty( "Elements" ).GetValue( sel, null ) ).Cast() + .ToList(); + } } - else + catch { } + return value; + } + public static void SetSelection2( this Selection + sel, Document doc, ICollection elementos ) + { + try { - PropertyInfo prop = sel.GetType().GetProperty( "Elements" ); - object temp = prop.GetValue( sel, null ); - - if( elementos.Count == 0 ) + sel.ClearSelection2(); + object[] parametros = new object[] { + elementos }; + Type[] tipos = parametros.Select( a => a + .GetType() ).ToArray(); + MethodInfo met = sel.GetType() + .GetMethod( "SetElementIds", tipos ); + if( met != null ) { - met = temp.GetType().GetMethod( "Clear" ); - met.Invoke( temp, null ); + met.Invoke( sel, parametros ); } else { - foreach( ElementId id in elementos ) + PropertyInfo prop = sel.GetType() + .GetProperty( "Elements" ); + object temp = prop.GetValue( sel, null ); + if( elementos.Count == 0 ) { - Element elemento = doc.PegarElemento( id ); - - - parametros = new object[] { elemento }; - tipos = parametros.Select( a => a.GetType() ).ToArray(); - met = temp.GetType().GetMethod( "Add", tipos ); - met.Invoke( temp, parametros ); + met = temp.GetType() + .GetMethod( "Clear" ); + met.Invoke( temp, null ); + } + else + { + foreach( ElementId id in elementos ) + { + Element elemento = doc + .GetElement2( id ); + parametros = new object[] { + elemento }; + tipos = parametros + .Select( a => a.GetType() ).ToArray(); + met = temp.GetType() + .GetMethod( "Add", tipos ); + met.Invoke( temp, parametros ); + } } } - } - - - - + catch { } } - public static void LimparSeleção( this Selection sel ) + public static void ClearSelection2( this + Selection sel ) { - //sel.Elements.Clear(); - //sel.SetElementIds(new List()); - PropertyInfo prop = sel.GetType().GetProperty( "Elements" ); - if( prop != null ) - { - object obj = prop.GetValue( sel, null ); - MethodInfo met = obj.GetType().GetMethod( "Clear" ); - met.Invoke( obj, null ); - } - else + try { - ICollection ids = new List(); - MethodInfo met = sel.GetType().GetMethod( "", new Type[] { ids.GetType() } ); - met.Invoke( sel, new object[] { ids } ); + PropertyInfo prop = sel.GetType() + .GetProperty( "Elements" ); + if( prop != null ) + { + object obj = prop.GetValue( sel, null ); + MethodInfo met = obj.GetType() + .GetMethod( "Clear" ); + met.Invoke( obj, null ); + } + else + { + ICollection ids = + new List(); + MethodInfo met = sel.GetType() + .GetMethod( "SetElementIds", new Type[] { ids.GetType() } ); + met.Invoke( sel, new object[] { ids } ); + } } + catch { } } + #endregion // selection - public static ICollection PegarSeleção( this Selection sel ) + #region uiapplication + public static System.Drawing + .Rectangle GetDrawingArea2( this UIApplication ui ) { - return null; - + System.Drawing.Rectangle value = System + .Windows.Forms.Screen.PrimaryScreen.Bounds; + try + { + } + catch { } + return value; } + #endregion // uiapplication - public static void Deletar( this Document doc, Element ele ) + #region view + public static ElementId Duplicate2( this View view ) { - //g.doc.Delete(ele); - //g.doc.Delete(ele.Id); - - object obj = doc; - MethodInfo met = obj.GetType().GetMethod( "Delete", new Type[] { typeof( Element ) } ); - if( met != null ) + ElementId value = null; + try { - met.Invoke( obj, new object[] { ele } ); + Document doc = view.Document; + List ls = doc.GetType().Assembly + .GetTypes().Where( a => a.IsEnum && a + .Name == "ViewDuplicateOption" ).ToList(); + if( ls.Count > 0 ) + { + Type t = ls[0]; + object obj = view; + MethodInfo met = view.GetType() + .GetMethod( "Duplicate", new Type[] { t } ); + if( met != null ) + { + value = met.Invoke( obj, + new object[] { 2 } ) as ElementId; + } + } } - else + catch { } + return value; + } + public static void SetOverlayView2( this View + view, List ids, Color cor = null, + int espessura = -1 ) + { + try { - met = obj.GetType().GetMethod( "Delete", new Type[] { typeof( ElementId ) } ); - met.Invoke( obj, new object[] { ele.Id } ); + Document doc = view.Document; + List ls = doc.GetType().Assembly + .GetTypes().Where( a => a.IsClass && a + .Name == "OverrideGraphicSettings" ).ToList(); + if( ls.Count > 0 ) + { + Type t = ls[0]; + ConstructorInfo construtor = t + .GetConstructor( new Type[] { } ); + construtor.Invoke( new object[] { } ); + object obj = construtor + .Invoke( new object[] { } ); + MethodInfo met = obj.GetType() + .GetMethod( "SetProjectionLineColor", new Type[] { cor + .GetType() } ); + met.Invoke( obj, new object[] { cor } ); + met = obj.GetType() + .GetMethod( "SetProjectionLineWeight", new Type[] { espessura + .GetType() } ); + met.Invoke( obj, + new object[] { espessura } ); + met = view.GetType() + .GetMethod( "SetElementOverrides", new Type[] { typeof(ElementId), + obj.GetType() } ); + foreach( ElementId id in ids ) + { + met.Invoke( view, + new object[] { id, obj } ); + } + } + else + { + MethodInfo met = view.GetType() + .GetMethod( "set_ProjColorOverrideByElement", + new Type[] { typeof( ICollection ), typeof( Color ) } ); + met.Invoke( view, new object[] { ids, + cor } ); + met = view.GetType() + .GetMethod( "set_ProjLineWeightOverrideByElement", + new Type[] { typeof( ICollection ), typeof( int ) } ); + met.Invoke( view, new object[] { ids, + espessura } ); + } } + catch { } } + #endregion // view - public static ElementId Duplicar( this View v ) + #region viewplan + public static ElementId GetViewTemplateId2( this + ViewPlan view ) { - ElementId id = null; - - //id = v.Duplicate(ViewDuplicateOption.WithDetailing); - + ElementId value = null; try { - - //JoinGeometryUtils.UnjoinGeometry(document, firstElement, secondElement); - List ls = doc.GetType().Assembly.GetTypes().Where( a => a.IsEnum && a.Name == "ViewDuplicateOption" ).ToList(); - - //if (ls.Count > 0) - //{ - // Type t = ls[0]; - - // MemberInfo[] memberInfos = t.GetMembers(BindingFlags.Public | BindingFlags.Static); - // string alerta = ""; - // for (int i = 0; i < memberInfos.Length; i++) - // { - // //WithDetailing; - // alerta += memberInfos[i].Name + " - "; - // alerta += memberInfos[i].GetType().Name + "\n"; - // } - - // object obj = v; - // MethodInfo met = v.GetType().GetMethod("Duplicate", new Type[] { typeof(ViewDuplicateOption) }); - // if (met != null) - // { - // id = met.Invoke(obj, new object[] { ViewDuplicateOption.WithDetailing }) as ElementId; - // } - //} + PropertyInfo prop = view.GetType() + .GetProperty( "ViewTemplateId" ); + if( prop != null ) + { + value = prop.GetValue( view, + null ) as ElementId; + } + } + catch { } + return value; + } + public static void SetViewTemplateId2( this + ViewPlan view, ElementId id ) + { + try + { + PropertyInfo prop = view.GetType() + .GetProperty( "ViewTemplateId" ); + if( prop != null ) + { + prop.SetValue( view, id, null ); + } } - catch( Exception ex ) + catch { } + } + #endregion // viewplan + + #region wall + public static void FlipWall2( this Wall wall ) + { + try { - //mbox.TryCatch(ex); + string metodo = "Flip"; + MethodInfo met = typeof( Wall ) + .GetMethod( metodo ); + if( met != null ) + { + met.Invoke( wall, null ); + } + else + { + metodo = "flip"; + met = typeof( Wall ).GetMethod( metodo ); + met.Invoke( wall, null ); + } } - return id; + catch { } } -#endif // _THESE_METHODS_CAUSE_COMPILER_ERRORS + #endregion // wall } - #endregion // Compatibilty Methods by Magson Leone + #endregion // Compatibility Methods by Magson Leone }