Client/Desktop/KMBIM3.0/KMBIM3.0_소스/Cmd/BomTable/FormBom.cs

204 lines
6.7 KiB
C#

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace KMBIM.Revit.Tools.Cmd.BomTable
{
public partial class FormBom : System.Windows.Forms.Form
{
UIDocument uidoc = null;
IList<SchedulableField> attlst = null;
SortedList<string, Category> myCategories = new SortedList<string, Category>();
public Dictionary<ElementId, List<SchedulableField>> SchCategories = new Dictionary<ElementId, List<SchedulableField>>();
public FormBom(UIDocument udoc)
{
InitializeComponent();
uidoc = udoc;
}
private void FormBom_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
Document doc = uidoc.Document;
Categories categories = doc.Settings.Categories;
foreach (Category c in categories)
{
myCategories.Add(c.Name, c);
}
listBox_source.Items.Clear();
foreach (Category c in categories)
{
if (c.AllowsBoundParameters)
listBox_source.Items.Add(c.Name);
}
}
private void button1_Click(object sender, EventArgs e)
{
string item = listBox_source.SelectedItem.ToString();
if (item == "") return;
listBox_target.Items.Add(item);
listBox_source.Items.Remove(item);
var category = myCategories[item];
SchCategories.Add(category.Id, new List<SchedulableField>());
}
private void listBox_target_SelectedIndexChanged(object sender, EventArgs e)
{
string item = listBox_target.SelectedItem.ToString();
if (item == "") return;
Transaction t = new Transaction(uidoc.Document, "Adding Schedules");
t.Start();
listBox_attribute.Items.Clear();
listBox_selAtt.Items.Clear();
var category = myCategories[item];
ViewSchedule schedule = ViewSchedule.CreateSchedule(uidoc.Document, new ElementId(category.Id.IntegerValue), ElementId.InvalidElementId);
attlst = schedule.Definition.GetSchedulableFields();
foreach (SchedulableField schedulableField in schedule.Definition.GetSchedulableFields())
{
//Judge if the FieldType is ScheduleFieldType.Instance.
if (schedulableField.FieldType == ScheduleFieldType.Instance)
{
//Get ParameterId of SchedulableField.
ElementId parameterId = schedulableField.ParameterId;
listBox_attribute.Items.Add(schedulableField.GetName(uidoc.Document).ToString());
}
}
foreach(var row in SchCategories[category.Id])
{
listBox_selAtt.Items.Add(row.GetName(uidoc.Document));
}
uidoc.Document.Delete(schedule.Id);
t.Commit();
}
private void button2_Click(object sender, EventArgs e)
{
string item = listBox_target.SelectedItem.ToString();
if (item == "") return;
listBox_source.Items.Add(item);
listBox_target.Items.Remove(item);
SchCategories.Remove(myCategories[item].Id);
}
private void button4_Click(object sender, EventArgs e)
{
string category_name = listBox_target.SelectedItem.ToString();
string item = listBox_attribute.SelectedItem.ToString();
if (item == "") return;
if (category_name == "") return;
listBox_selAtt.Items.Add(item);
listBox_attribute.Items.Remove(item);
var row = attlst
.Cast<SchedulableField>()
.SingleOrDefault(i => i.GetName(uidoc.Document) == item);
if (row != null)
SchCategories[myCategories[category_name].Id].Add(row);
}
private void button3_Click(object sender, EventArgs e)
{
string category_name = listBox_target.SelectedItem.ToString();
string item = listBox_selAtt.SelectedItem.ToString();
if (item == "") return;
if (category_name == "") return;
listBox_attribute.Items.Add(item);
listBox_selAtt.Items.Remove(item);
var row = attlst
.Cast<SchedulableField>()
.SingleOrDefault(i => i.GetName(uidoc.Document) == item);
if (row != null)
SchCategories[myCategories[category_name].Id].Remove(row);
}
private void button_up_Click(object sender, EventArgs e)
{
MoveItem(-1, listBox_selAtt);
}
private void button_down_Click(object sender, EventArgs e)
{
MoveItem(1, listBox_selAtt);
}
public void MoveItem(int direction, ListBox listBox)
{
// Checking selected item
if (listBox.SelectedItem == null || listBox.SelectedIndex < 0)
return; // No selected item - nothing to do
int originIndex = listBox.SelectedIndex;
// Calculate new index using move direction
int newIndex = listBox.SelectedIndex + direction;
// Checking bounds of the range
if (newIndex < 0 || newIndex >= listBox.Items.Count)
return; // Index out of range - nothing to do
object selected = listBox.SelectedItem;
// Removing removable element
listBox.Items.Remove(selected);
// Insert it in new position
listBox.Items.Insert(newIndex, selected);
// Restore selection
listBox.SetSelected(newIndex, true);
string item = listBox_target.SelectedItem.ToString();
var category = myCategories[item];
// KDCS.Utils.Util.Swap(SchCategories[category], originIndex, newIndex);
}
private void listBox_attribute_SelectedIndexChanged(object sender, EventArgs e)
{
//listBox_selAtt.Items.Clear();
}
private void listBox_source_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void FormBom_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.DialogResult = DialogResult.Cancel;
}
else if (e.KeyCode == Keys.Enter)
{
this.DialogResult = DialogResult.OK;
}
}
}
}