Skip to content

Instantly share code, notes, and snippets.

@SpacePurr
Last active January 21, 2020 14:40
Show Gist options
  • Save SpacePurr/63e74f7a7bb4f79f145170e56f8c6253 to your computer and use it in GitHub Desktop.
Save SpacePurr/63e74f7a7bb4f79f145170e56f8c6253 to your computer and use it in GitHub Desktop.
Покраска цвета Xbim 3D Control
//Покрасить элеменет
public void ChangeColorInIfc(string currentColor, IfcProduct ifcProd)
{
int entityLabel = ifcProd.EntityLabel;
//Метод сохранения
//Передаваться будет только выбранный цвет из Dictionary StatusColors
using (var txn = Model.BeginTransaction("Set Style"))
{
//Получаем сет CSoft_Colors
var colors = ifcProd.PropertySets.FirstOrDefault(ps => ps.Name == "CSoft_Colors");
//Старый стиль
IfcStyledItem styledItem = null;
//Новый стиль
IfcStyledItem newStyledItem;
IfcPropertySingleValue baseStyleAssigmentProp;
IfcPropertySingleValue baseColorRgbProp;
IfcPropertySingleValue baseStyledItemProp;
if (colors == null)
{
var repItemsLabels = ifcProd.Representation.Representations.LastOrDefault();
var body = repItemsLabels.Items.FirstOrDefault();
//Если у элемента есть стиль
if (body.StyledByItem.Count() != 0)
{
styledItem = body.StyledByItem.First();
styledItem.Item = null;
styledItem.Name = $"BaseStyle;EntityLabel:{entityLabel}";
IfcPresentationStyleAssignment styleAssigment = styledItem.Styles.FirstOrDefault();
IfcSurfaceStyle surfaceStyle = (IfcSurfaceStyle)styleAssigment.Styles.FirstOrDefault();
IfcSurfaceStyleRendering surfaceStyleRendering = (IfcSurfaceStyleRendering)surfaceStyle.Styles.FirstOrDefault();
IfcColourRgb colorRgb = surfaceStyleRendering.SurfaceColour;
newStyledItem = Model.Instances.New<IfcStyledItem>();
newStyledItem.Name = $"NewStyle;EntityLabel:{entityLabel};BaseStyledItem:{styledItem.EntityLabel}";
newStyledItem.Item = body;
newStyledItem.Styles.Add((IfcPresentationStyleAssignment)StatusColors[currentColor][0]);
baseStyledItemProp = Model.Instances.New<IfcPropertySingleValue>(p =>
{
p.Name = "BaseStyle";
p.NominalValue = new IfcLabel(styledItem.EntityLabel.ToString());
});
baseStyleAssigmentProp = Model.Instances.New<IfcPropertySingleValue>(p =>
{
p.Name = "BaseStyleAssigment";
p.NominalValue = new IfcLabel(styleAssigment.EntityLabel.ToString());
});
baseColorRgbProp = Model.Instances.New<IfcPropertySingleValue>(p =>
{
p.Name = "BaseColorRgb";
p.NominalValue = new IfcLabel($"R:{colorRgb.Red};G:{colorRgb.Green};B:{colorRgb.Blue}");
});
}
//Если у элемента стиля нет
else
{
newStyledItem = Model.Instances.New<IfcStyledItem>();
newStyledItem.Name = $"NewStyleWithoutBase;EntityLabel:{entityLabel}";
newStyledItem.Item = body;
newStyledItem.Styles.Add((IfcPresentationStyleAssignment)StatusColors[currentColor][0]);
baseStyledItemProp = Model.Instances.New<IfcPropertySingleValue>(p =>
{
p.Name = "BaseStyle";
p.NominalValue = new IfcLabel();
});
baseStyleAssigmentProp = Model.Instances.New<IfcPropertySingleValue>(p =>
{
p.Name = "BaseStyleAssigment";
p.NominalValue = new IfcLabel();
});
baseColorRgbProp = Model.Instances.New<IfcPropertySingleValue>(p =>
{
XbimColourMap map = new XbimColourMap();
var color = map[ifcProd.GetType().Name];
p.Name = "BaseColorRgb";
p.NominalValue = new IfcLabel($"R:{color.Red};G:{color.Green};B:{color.Blue}");
});
}
var newStyledItemProp = Model.Instances.New<IfcPropertySingleValue>(p =>
{
p.Name = "NewStyle";
p.NominalValue = new IfcLabel(newStyledItem.EntityLabel.ToString());
});
var newStyleAssigmentProp = Model.Instances.New<IfcPropertySingleValue>(p =>
{
p.Name = "NewStyleAssigment";
p.NominalValue = new IfcLabel(((IfcPresentationStyleAssignment)StatusColors[currentColor][0]).EntityLabel.ToString());
});
var newColorRgbProp = Model.Instances.New<IfcPropertySingleValue>(p =>
{
IfcColourRgb color = (IfcColourRgb)StatusColors[currentColor][1];
p.Name = "NewColorRgb";
p.NominalValue = new IfcLabel($"R:{color.Red};G:{color.Green};B:{color.Blue}");
});
colors = Model.Instances.New<IfcPropertySet>(ps =>
{
ps.Name = "CSoft_Colors";
ps.HasProperties.Add(baseStyledItemProp);
ps.HasProperties.Add(baseStyleAssigmentProp);
ps.HasProperties.Add(baseColorRgbProp);
ps.HasProperties.Add(newStyledItemProp);
ps.HasProperties.Add(newStyleAssigmentProp);
ps.HasProperties.Add(newColorRgbProp);
});
ifcProd.AddPropertySet(colors);
}
else
{
var repItemsLabels = ifcProd.Representation.Representations.LastOrDefault();
var body = repItemsLabels.Items.FirstOrDefault();
newStyledItem = ifcProd.Model.Instances.OfType<IfcStyledItem>().Where(x => x.Item == body).FirstOrDefault();
IfcPropertySingleValue newStyledItemEntityLabel = (IfcPropertySingleValue)colors.HasProperties.FirstOrDefault(p => p.Name == "NewStyle");
newStyledItem.Styles.Clear();
newStyledItem.Styles.Add((IfcPresentationStyleAssignment)StatusColors[currentColor][0]);
IfcPropertySingleValue newStyleAssigmentProp = (IfcPropertySingleValue)colors.HasProperties.FirstOrDefault(p => p.Name == "NewStyleAssigment");
newStyleAssigmentProp.NominalValue = new IfcLabel(((IfcPresentationStyleAssignment)StatusColors[currentColor][0]).EntityLabel.ToString());
IfcColourRgb color = (IfcColourRgb)StatusColors[currentColor][1];
IfcPropertySingleValue newColorRgb = (IfcPropertySingleValue)colors.HasProperties.FirstOrDefault(p => p.Name == "NewColorRgb");
newColorRgb.NominalValue = new IfcLabel($"R:{color.Red};G:{color.Green};B:{color.Blue}");
}
txn.Commit();
}
}
//Вернуть стиль
public MySurfaceLayerStyler ReturnStyleMethod(EntitySelection SelectedEntities, MySurfaceLayerStyler styler)
{
foreach (IfcProduct entity in SelectedEntities)
{
int entityLabel = entity.EntityLabel;
using (var txn = Model.BeginTransaction("Return Style"))
{
IfcPropertySet colors = entity.PropertySets.FirstOrDefault(p => p.Name == "CSoft_Colors");
if (colors != null)
{
IfcPropertySingleValue color = (IfcPropertySingleValue)colors.HasProperties.FirstOrDefault(p => p.Name == "BaseColorRgb");
string nominalValue = color.NominalValue.ToString().Replace('.', ',');
string[] rgb = nominalValue.Split(';');
styler.ForcingColorEntityTo(entity, new XbimColour("", double.Parse(rgb[0].Split(':')[1]), double.Parse(rgb[1].Split(':')[1]), double.Parse(rgb[2].Split(':')[1])));
IfcPropertySingleValue baseStyleProp = (IfcPropertySingleValue)colors.HasProperties.FirstOrDefault(p => p.Name == "BaseStyle");
IfcPropertySingleValue newStyleProp = (IfcPropertySingleValue)colors.HasProperties.FirstOrDefault(p => p.Name == "NewStyle");
IfcStyledItem newStyledItem = Model.Instances.OfType<IfcStyledItem>().FirstOrDefault(s => s.EntityLabel.ToString() == newStyleProp.NominalValue.ToString()); ;
if (baseStyleProp.NominalValue.ToString() != "")
{
IfcStyledItem styledItem = Model.Instances.OfType<IfcStyledItem>().FirstOrDefault(s => s.EntityLabel.ToString() == baseStyleProp.NominalValue.ToString());
styledItem.Item = newStyledItem.Item;
styledItem.Name = null;
}
//var ownerHistory = colors.OwnerHistory; //IfcOwnerHistory
//var personAndOrganization = ownerHistory.OwningUser; //IfcPersonAndOrganization
//var person = personAndOrganization.ThePerson; //IfcPerson
//var organization = personAndOrganization.TheOrganization; //IfcOrganization
//var application = ownerHistory.OwningApplication; //IfcApplication
//var relDefinesByProperties = Model.Instances.OfType<IfcRelDefinesByProperties>().FirstOrDefault(p => p.RelatingPropertyDefinition == colors); //IfcRelDefinesByProperties
for (int i = 0; i < colors.HasProperties.Count;)
{
Model.Delete(colors.HasProperties[i]);
}
Model.Delete(newStyledItem);
Model.Delete(colors);
Model.Delete(newStyledItem);
}
txn.Commit();
}
}
return styler;
}
//Создать базовые цвета в ifc
private Dictionary<string, object[]> CreateDictionaryStatusColors()
{
Dictionary<string, object[]> statusColors = new Dictionary<string, object[]>();
using (var txn = Model.BeginTransaction("Create base colors"))
{
var colors = Model.Instances.OfType<IfcColourRgb>().Where(c =>
c.Name == "CSoft_Red" ||
c.Name == "CSoft_Green" ||
c.Name == "CSoft_Blue");
if (colors.Count() == 0)
{
statusColors.Add("CSoft_Red", CreatePresentationStyleAssigment(Color.Red));
statusColors.Add("CSoft_Green", CreatePresentationStyleAssigment(Color.Green));
statusColors.Add("CSoft_Blue", CreatePresentationStyleAssigment(Color.Blue));
}
else
{
statusColors.Add("CSoft_Red", GetPresentationStyleAssigment(colors.Where(c => c.Name == "CSoft_Red").First()));
statusColors.Add("CSoft_Green", GetPresentationStyleAssigment(colors.Where(c => c.Name == "CSoft_Green").First()));
statusColors.Add("CSoft_Blue", GetPresentationStyleAssigment(colors.Where(c => c.Name == "CSoft_Blue").First()));
}
txn.Commit();
}
return statusColors;
}
//Создать стиль
private object[] CreatePresentationStyleAssigment(Color statusColor)
{
try
{
var color = Model.Instances.New<IfcColourRgb>();
color.Name = $"CSoft_{statusColor.Name}";
color.Red = statusColor.R;
color.Green = statusColor.G;
color.Blue = statusColor.B;
var newStyleRendering = Model.Instances.New<IfcSurfaceStyleRendering>();
newStyleRendering.SurfaceColour = color;
var newSurfaceStyle = Model.Instances.New<IfcSurfaceStyle>();
newSurfaceStyle.Styles.Add(newStyleRendering);
var newStyleAssignment = Model.Instances.New<IfcPresentationStyleAssignment>();
newStyleAssignment.Styles.Add(newSurfaceStyle);
object[] stylesParameters = new object[2];
stylesParameters[0] = newStyleAssignment;
stylesParameters[1] = color;
return stylesParameters;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return null;
}
}
//Получить стиль
private object[] GetPresentationStyleAssigment(IfcColourRgb statusColor)
{
var styleRendering = Model.Instances.OfType<IfcSurfaceStyleRendering>().Where(sr => sr.SurfaceColour == statusColor);
var surfaceStyle = Model.Instances.OfType<IfcSurfaceStyle>().Where(ss => ss.Styles.FirstOrDefault() == styleRendering.First());
var styleAssigment = Model.Instances.OfType<IfcPresentationStyleAssignment>().Where(ps => ps.Styles.FirstOrDefault() == surfaceStyle.First());
object[] stylesParameters = new object[2];
stylesParameters[0] = styleAssigment.First();
stylesParameters[1] = statusColor;
return stylesParameters;
}
//Команда установить стиль
private void SetStyleMethod(object obj)
{
if (SelectedEntities.Count() == 0)
return;
//Открываем диалог выбора цвета
ColorBox box = new ColorBox();
ColorBoxViewModel boxVm = box.DataContext as ColorBoxViewModel;
box.ShowDialog();
Color statusColor = boxVm.StatusColor;
//Если нажата кнопка установить новый цвет
if (boxVm.ClickResult == 1 && !statusColor.IsEmpty)
{
//Имя цвета, записываемое в PropertySet
string currentColor = "CSoft_" + statusColor.Name;
//Покраска элементов модели в файле
//Каждый выбранный элемент записывается в очередь выбранных элементов
//а сам процесс покраски ChangeColorInIfc записывается в очередь процессов
foreach (IfcProduct entity in SelectedEntities)
{
ifcProducts.Enqueue(entity);
taskQueue.Queue(() => Task.Run(() => master.ChangeColorInIfc(currentColor, entity)));
}
taskQueue.ProcessBackground();
//Покраска элементов модели в слое
foreach (IPersistEntity entity in SelectedEntities)
{
styler.ForcingColorEntityTo(entity, new XbimColour(currentColor, statusColor.R, statusColor.G, statusColor.B));
}
DrawingControl.DefaultLayerStyler = styler;
ReloadModel();
}
//Если нажата кнопка установить базовый цвет
else if (((ColorBoxViewModel)box.DataContext).ClickResult == 2)
{
DrawingControl.DefaultLayerStyler = master.ReturnStyleMethod(SelectedEntities, styler);
ReloadModel();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment