Code from today’s #RTCEUR advanced API course

Today’s course focused on how to use custom classes, overrides, custom enums, source code control (with https://bitbucket.org & https://www.sourcetreeapp.com) and more. Below is the code we developed during the class.

public void groupAudit()
{
    Document doc = this.ActiveUIDocument.Document;
    
    List<Group> groups = new FilteredElementCollector(doc).OfClass(typeof(Group)).Cast<Group>().ToList();
    List<GroupType> groupTypes = new FilteredElementCollector(doc).OfClass(typeof(GroupType)).Cast<GroupType>().ToList();
    
    List<Tuple<string, int, int>> groupData = new List<Tuple<string, int, int>>();
    
//			using (StreamWriter sw = new StreamWriter(@"C:\Users\harry_000\Desktop\RTC_EUR_2015-2015-10-28\RTC EUR 2015\Advanced API\\output.txt"))
//			{
        foreach (GroupType groupType in groupTypes)
        {
            //int numberOfElements = group.GetMemberIds().Count;
            
            int numberOfElementsInGroupType = 0;
            
//				foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(Group)))
//				{
//					Group g = element as Group;
//				}
            
            // old way without LINQ
//				foreach (Group group in new FilteredElementCollector(doc).OfClass(typeof(Group)).Cast<Group>())
//				{
//					if (group.GroupType.Id == groupType.Id)
//					{
//						countA = group.GetMemberIds().Count;
//						continue;
//					}
//				}
            
            // with LINQ - better
            foreach (Group group in new FilteredElementCollector(doc).OfClass(typeof(Group))
                     .Cast<Group>()
                     .Where(g => g.GroupType.Id == groupType.Id))
            {
                numberOfElementsInGroupType = group.GetMemberIds().Count;
            }
            
            int numberOfGroupInstances = new FilteredElementCollector(doc).OfClass(typeof(Group)).Cast<Group>()
                .Where(g => g.GroupType != null && g.GroupType.Id == groupType.Id).Count();
            
            TaskDialog.Show("number", groupType.Name + " = " + numberOfElementsInGroupType.ToString()
                           + Environment.NewLine 
                          + numberOfGroupInstances);
            
            // not good to mix collecting the data and using the data in the same loop
            // instead, better to collect the data, store it, and then use it later
            //sw.WriteLine(groupType.Name + ", " + numberOfGroupInstances + "," + numberOfElementsInGroupType);
            
            groupData.Add(new Tuple<string, int, int>(groupType.Name, numberOfGroupInstances, numberOfElementsInGroupType));
            
            //int count = new FilteredElementCollector(doc).OfClass(typeof(Group)).Cast<Group>()
        }
        
        //writeTupleToFile(groupData);

        
//	}
}

public void getOfficeGroupInfo()
{
    Document doc = this.ActiveUIDocument.Document;
    namedGroupDataImplementation(doc, GroupNamePrefix.Office);
}

public void getKitchenGroupInfo()
{
    Document doc = this.ActiveUIDocument.Document;
    namedGroupDataImplementation(doc, GroupNamePrefix.Kitchen);
}


public void namedGroupDataImplementation(Document doc, GroupNamePrefix prefix)
{
    
}

public enum GroupNamePrefix
{
Office,
Kitchen,
Bath
};

public void getDetailGroupInfo()
{
    Document doc = this.ActiveUIDocument.Document;
    groupDataImplementation(doc, BuiltInCategory.OST_IOSDetailGroups);
}

public void getModelGroupInfo()
{
    Document doc = this.ActiveUIDocument.Document;
    groupDataImplementation(doc, BuiltInCategory.OST_IOSModelGroups);
}

public void groupDataImplementation(Document doc, BuiltInCategory cat)
{
    
    List<Group> groups = new FilteredElementCollector(doc).OfClass(typeof(Group)).Cast<Group>().ToList();
    List<GroupType> groupTypes = new FilteredElementCollector(doc).OfClass(typeof(GroupType)).Cast<GroupType>()
        .ToList();
    
    // store data in a list of GroupData objects instead of a Tuple
    List<GroupData> groupData = new List<GroupData>();
    int numberOfElementsInGroupType = 0;
    foreach (GroupType groupType in groupTypes)
    {
        // homework - use the temporary transaction trick for group types with no group instances
// http://thebuildingcoder.typepad.com/blog/2012/11/temporary-transaction-trick-touchup.html				
        
        if (new FilteredElementCollector(doc).OfClass(typeof(Group)).Cast<Group>()
            .FirstOrDefault(q => q.GroupType.Id == groupType.Id && q.Category.Id.IntegerValue == (int)cat) == null)
            continue;
        
        foreach (Group group in new FilteredElementCollector(doc).OfClass(typeof(Group))
                 .Cast<Group>()
                 .Where(g => g.GroupType.Id == groupType.Id))
        {
            numberOfElementsInGroupType = group.GetMemberIds().Count;
            
            foreach (ElementId id in group.GetMemberIds())
            {
                Element e = doc.GetElement(id);
            }
            
        }
        
        int numberOfGroupInstances = new FilteredElementCollector(doc).OfClass(typeof(Group)).Cast<Group>()
            .Where(g => g.GroupType != null && g.GroupType.Id == groupType.Id).Count();
        
        groupData.Add(new GroupData(groupType.Name, numberOfGroupInstances, numberOfElementsInGroupType));
    }
    writeGroupDataToFile(groupData);
}



public void groupAuditUsingCustomClassToStoreData()
{
    Document doc = this.ActiveUIDocument.Document;
    
    List<Group> groups = new FilteredElementCollector(doc).OfClass(typeof(Group)).Cast<Group>().ToList();
    List<GroupType> groupTypes = new FilteredElementCollector(doc).OfClass(typeof(GroupType)).Cast<GroupType>().ToList();
    
    // store data in a list of GroupData objects instead of a Tuple
    List<GroupData> groupData = new List<GroupData>();
    int numberOfElementsInGroupType = 0;
    foreach (GroupType groupType in groupTypes)
    {
        foreach (Group group in new FilteredElementCollector(doc).OfClass(typeof(Group))
                 .Cast<Group>()
                 .Where(g => g.GroupType.Id == groupType.Id))
        {
            numberOfElementsInGroupType = group.GetMemberIds().Count;
        }
        
        int numberOfGroupInstances = new FilteredElementCollector(doc).OfClass(typeof(Group)).Cast<Group>()
            .Where(g => g.GroupType != null && g.GroupType.Id == groupType.Id).Count();
        
//				GroupData data = new GroupData();
//				data.name = groupType.Name;
//				data.numberOfElements = numberOfElementsInGroupType;
//				data.numberOfInstances = numberOfGroupInstances;
//				groupData.Add(data);
        
        //GroupData groupDataWithConstructor = new GroupData(groupType.Name, numberOfGroupInstances, numberOfElementsInGroupType);
        groupData.Add(new GroupData(groupType.Name, numberOfGroupInstances, numberOfElementsInGroupType));
        
        //groupData.Add(new Tuple<string, int, int>(groupType.Name, numberOfGroupInstances, numberOfElementsInGroupType));
    }
        
    writeGroupDataToFile(groupData);
    
    
}

public class GroupData
{
    public GroupData() {}
    
    public GroupData(string _Name, int NumberOfInstances, int NumberOfElements)
    {
        name = _Name;
        numberOfInstances = NumberOfInstances;
        numberOfElements = NumberOfElements;
    }
    public string name;
    public int numberOfInstances;
    public int numberOfElements;
}

private void writeGroupDataToFile(List<GroupData> data)
{
    // now use the data that was previously stored
    
    string filename = @"C:\Users\harry_000\Desktop\RTC_EUR_2015-2015-10-28\RTC EUR 2015\Advanced API\\output.txt";
    using (StreamWriter sw = new StreamWriter(filename))
    {
        foreach (GroupData gd in data)
        {
            sw.WriteLine(gd.name + "," + gd.numberOfElements + "," + gd.numberOfInstances);
        }
    }
    Process.Start(filename);
}

private void writeTupleToFile(List<Tuple<string, int, int>> data)
{
    // now use the data that was previously stored
    using (StreamWriter sw = new StreamWriter(@"C:\Users\harry_000\Desktop\RTC_EUR_2015-2015-10-28\RTC EUR 2015\Advanced API\\output.txt"))
    {
        foreach (Tuple<string, int, int> tup in data)
        {
            sw.WriteLine(tup.Item1 + "," + tup.Item2 + "," + tup.Item3);
        }
    }
}


private void writeTupleToFile(List<Tuple<string, int, int, int>> data)
{
    // now use the data that was previously stored
    using (StreamWriter sw = new StreamWriter(@"C:\Users\harry_000\Desktop\RTC_EUR_2015-2015-10-28\RTC EUR 2015\Advanced API\\output.txt"))
    {
        foreach (Tuple<string, int, int, int> tup in data)
        {
            sw.WriteLine(tup.Item1 + "," + tup.Item2 + "," + tup.Item3 + "," + tup.Item4);
        }
    }
}
Advertisements

2 thoughts on “Code from today’s #RTCEUR advanced API course

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s