using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using WPFprog.ViewModel;
using WPFprog.Model;
using System.Linq;
namespace WPFprog.Model
{
internal class RevitModelClass
{
private UIApplication _uiApplication;
private Autodesk.Revit.ApplicationServices.Application _application;
private Document _document;
private readonly UIDocument _uiDocument;
public ElementId SelectedFamilyId;
public static bool containsDefinition(DefinitionGroup grp, string paramName)
{
paramName = paramName.ToLower();
foreach (Definition def in grp.Definitions)
if (string.Equals(def.Name.ToLower(), paramName))
return true;
return false;
}
public bool AddParametersToFamily(List<MyParameter> pars)
{
Element family = _document.GetElement(SelectedFamilyId);
if (SelectedFamilyId == null) return false;
if (family is FamilyInstance fi)
{
var collector = new FilteredElementCollector(_document).OfClass(typeof(Family))
.OfType<Family>()
.FirstOrDefault(x => x.Name == fi.Symbol.FamilyName);
var famDoc = _document.EditFamily(collector);
using (Transaction tr = new Transaction(famDoc, "addParameters"))
{
tr.Start();
foreach (var par in pars)
{
BuiltInParameterGroup gr = BuiltInParameterGroup.PG_GENERAL;
famDoc.FamilyManager.AddParameter(par.def, gr, true);
}
tr.Commit();
}
//famDoc.SaveAs("C:\\Users\\Александр\\Desktop\\Не получается.rfa");
famDoc.LoadFamily(_document, new FamilyLoadOptions());
famDoc.Close(false);
}
return false;
}
public RevitModelClass(UIApplication uiapp)
{
_uiApplication = uiapp;
_application = _uiApplication.Application;
_uiDocument = _uiApplication.ActiveUIDocument;
_document = _uiDocument.Document;
}
public static ObservableCollection<MyParameter> GenerateParametersList(Document doc)
{
ObservableCollection<MyParameter> definitions = new ObservableCollection<MyParameter>();
var deffile = doc.Application.OpenSharedParameterFile();
if (deffile != null)
{
foreach (var gr in deffile.Groups)
{
foreach (ExternalDefinition df in gr.Definitions)
{
definitions.Add(new MyParameter() { Name = df.Name, def = df });
}
}
}
return definitions;
}
class FamilyLoadOptions : IFamilyLoadOptions
{
public bool OnFamilyFound(bool familyInUse, out bool overwriteParameterValues)
{
overwriteParameterValues = true;
return true;
}
public bool OnSharedFamilyFound(Family sharedFamily, bool familyInUse, out FamilySource source, out bool overwriteParameterValues)
{
source = FamilySource.Family;
overwriteParameterValues = true;
return true;
}
}
}
}