sharp-wawi/models/PartAssembly.cs

90 lines
2.6 KiB
C#
Raw Normal View History

2018-07-30 17:55:15 +02:00
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
2018-08-01 23:33:10 +02:00
using mapper.attributes;
2018-07-30 17:55:15 +02:00
namespace sharpwawi.models
{
2018-08-01 23:33:10 +02:00
[MappingConstraintAttribute(FieldMappingConstraint = MappingConstraints.DONTMAP,PropertyMappingConstraint = MappingConstraints.ANNOTATEDONLY)]
2018-09-12 21:29:58 +02:00
public class PartAssembly
2018-07-30 17:55:15 +02:00
{
2018-08-01 23:33:10 +02:00
[Mapped]
[PrimaryKey]
2018-09-12 21:29:58 +02:00
public Part Part;
2018-08-01 23:33:10 +02:00
[Mapped]
[PrimaryKey]
2018-09-12 21:29:58 +02:00
public int Revision;
2018-07-30 17:55:15 +02:00
IList<AssemblyComponent> components = new List<AssemblyComponent>();
2018-09-12 21:29:58 +02:00
protected PartAssembly()
2018-07-30 17:55:15 +02:00
{
}
2018-09-12 21:29:58 +02:00
internal PartAssembly(Part part)
2018-07-30 17:55:15 +02:00
{
Part = part;
Revision = part.HighestAssemblyRevision + 1;
}
public int Count => components.Count;
public AssemblyComponent this[int n] => components[n];
public AssemblyComponent this[Part component]{
get {
foreach (AssemblyComponent assemblyComponent in components)
{
if (assemblyComponent.Component.Equals(component))
return assemblyComponent;
}
throw new IndexOutOfRangeException();
}
}
public bool ContainsComponent(Part component){
foreach (AssemblyComponent assemblyComponent in components){
if (assemblyComponent.Component.Equals(component))
return true;
}
return false;
}
2018-09-12 21:29:58 +02:00
public PartAssembly Clone(){
PartAssembly clone = Part.CreateAssembly();
2018-07-30 17:55:15 +02:00
foreach (AssemblyComponent assemblyComponent in components)
clone.AddComponent(assemblyComponent.Component, assemblyComponent.Quantity);
return clone;
}
/**
* Add a qunatity of a component to the Assembly.
* Returns true if the component is new to the assembly, false if just the quantity changed
*
**/
public bool AddComponent(Part component, double quantity)
{
if (ContainsComponent(component))
{
this[component].Quantity += quantity;
return false;
} else {
components.Add(new AssemblyComponent(this, component, quantity));
return true;
}
}
public void RemoveComponent(AssemblyComponent component)
{
components.Remove(component);
}
public void RemoveComponent(Part component){
RemoveComponent(this[component]);
}
}
}