Hi Harshad,
Apologize for not clearing my statement. Right, I have created .msi file with the same code.
Actually, I have written a code for pinning and unpinning the batch category in revit and its working perfectly fine (its pinning or unpinning the selected batch categories) using revit addin manager and when I install this code in revit as an addin, it doing everything same but when I close the winform, its undoing the changes.
Below is the code for your reference.
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
namespace myFirstPlugin
{
public partial class PinCategory : System.Windows.Forms.Form
{
private Dictionary<string, BuiltInCategory> categoryMap;
private List<int> positions = new List<int>();
private Autodesk.Revit.DB.Document doc;
public PinCategory(Autodesk.Revit.DB.Document doc)
{
InitializeComponent();
this.doc = doc;
}
private void PinCategory_Load(object sender, EventArgs e)
{
// Retrieve all built-in categories
Array builtInCategories = Enum.GetValues(typeof(BuiltInCategory));
List<string> categoryNames = new List<string>();
categoryMap = new Dictionary<string, BuiltInCategory>();
foreach (var builtInCategory in builtInCategories)
{
BuiltInCategory category = (BuiltInCategory)builtInCategory;
string categoryName = category.ToString().Replace("OST_", "");
categoryNames.Add(categoryName);
categoryMap[categoryName] = category;
}
// Sort the category names alphabetically
categoryNames.Sort();
// Populate Categories_button with category names
Values_checkedlistbox.DataSource = categoryNames;
}
private void Values_checkedlistbox_SelectedIndexChanged_1(object sender, EventArgs e)
{
// Check if the shift key is pressed
if (System.Windows.Forms.Control.ModifierKeys == Keys.Shift)
{
// Get the selected index of the current item
int currentIndex = Values_checkedlistbox.SelectedIndex;
// Check if there are any previously selected items
if (positions.Count > 0)
{
// Get the last selected index
int lastIndex = positions[positions.Count - 1];
// Get the minimum and maximum indices
int minIndex = Math.Min(currentIndex, lastIndex);
int maxIndex = Math.Max(currentIndex, lastIndex);
// Select the range of items between the minimum and maximum indices
for (int i = minIndex; i <= maxIndex; i++)
{
Values_checkedlistbox.SetItemChecked(i, true);
}
}
else
{
// No previously selected items, so just select the current item
Values_checkedlistbox.SetItemChecked(currentIndex, true);
}
// Add the current index to the positions list
positions.Add(currentIndex);
}
}
private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
{
bool isAllChecked = checkBox1.Checked;
for (int i = 0; i < Values_checkedlistbox.Items.Count; i++)
{
Values_checkedlistbox.SetItemChecked(i, isAllChecked);
}
checkBox1.Checked = isAllChecked;
}
private void Pin_radionbutton_CheckedChanged_1(object sender, EventArgs e)
{
}
private void Unpin_radionbutton_CheckedChanged_1(object sender, EventArgs e)
{
}
private void Click_Click_1(object sender, EventArgs e)
{
// Get the selected radio button
bool pinSelected = Pin_radionbutton.Checked; // Replace with your actual pin radio button control
bool unpinSelected = Unpin_radionbutton.Checked; // Replace with your actual unpin radio button control
if (pinSelected || unpinSelected)
{
// Get the selected categories
List<string> selectedCategories = new List<string>();
foreach (var item in Values_checkedlistbox.CheckedItems)
{
selectedCategories.Add(item.ToString());
}
if (selectedCategories.Count > 0)
{
// Start a new transaction
using (Transaction trans = new Transaction(doc, "Pin/Unpin Elements"))
{
try
{
// Start the transaction
trans.Start();
// Retrieve elements of selected categories
List<ElementId> elementIds = new List<ElementId>();
foreach (var category in selectedCategories)
{
BuiltInCategory builtInCategory = categoryMap[category];
ElementCategoryFilter categoryFilter = new ElementCategoryFilter(builtInCategory);
FilteredElementCollector collector = new FilteredElementCollector(doc).WherePasses(categoryFilter);
elementIds.AddRange(collector.ToElementIds());
}
int totalCount = 0;
foreach (ElementId elementId in elementIds)
{
Element element = doc.GetElement(elementId);
try
{
// Pin the element
if (pinSelected && !element.Pinned)
{
element.Pinned = true;
totalCount++; // Increment the count of replaced values
}
// Unpin the element
if (unpinSelected && element.Pinned)
{
element.Pinned = false;
totalCount--; // Increment the count of replaced values
}
}
catch (Exception ex)
{
// Handle any exceptions that occur during the transaction
// You can display a message box or log the error
//MessageBox.Show($"Error occurred while pinning/unpinning element: {ex.Message}");
}
}
// Commit the transaction
trans.Commit();
// Show a message indicating the operation was successful
TaskDialog.Show("Success", $"{totalCount} Elements pinned/unpinned successfully.");
}
catch (Exception ex)
{
// Handle any exceptions that occur during the transaction
// You can display a message box or log the error
//MessageBox.Show($"Error occurred during the transaction: {ex.Message}");
// Roll back the transaction
trans.RollBack();
}
}
}
else
{
// Show a message indicating no categories were selected
TaskDialog.Show("Warning", "No categories selected.");
}
}
else
{
// Show a message indicating no pin/unpin option was selected
TaskDialog.Show("Warning", "Please select either Pin or Unpin option.");
}
}
private void OK_Click_1(object sender, EventArgs e)
{
this.Close();
}
private void label9_Click(object sender, EventArgs e)
{
}
}
}