Addin fail to implement the change after hitting 'OK' button

Addin fail to implement the change after hitting 'OK' button

lbhardwaj4ZUA6
Participant Participant
812 Views
9 Replies
Message 1 of 10

Addin fail to implement the change after hitting 'OK' button

lbhardwaj4ZUA6
Participant
Participant

I am using Revit addin manager and the code is running perfectly fine but when I run the .msi file which I created using that code and install it in my revit, it shows the winform exactly similar to what it was showing when working with revit addin manager and do the required changes as well but when I hit ok button which is "this. Ok" in the button event handler, its undoing the changes I have done. It is strange because the process is same but its not working in the revit.

 

Any idea why?

0 Likes
813 Views
9 Replies
Replies (9)
Message 2 of 10

Mohamed_Arshad
Advisor
Advisor

HI @lbhardwaj4ZUA6 
You having issue in MSI File Extracted Dll Right?

While Click on the OK Button, what changes is Happening exactly? Can you explain in detail.
While Click on the OK Button Plugin not working ?

Can you please answer the above question, So that it will help me in solving your query.


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 3 of 10

lbhardwaj4ZUA6
Participant
Participant
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)
{

}
}
}
0 Likes
Message 4 of 10

Mohamed_Arshad
Advisor
Advisor

HI @lbhardwaj4ZUA6 

In the Below Catch, Instead of Putting RollBack(), Use Some TaskDialog to See the Exception Message. I think due to some exception while pinning opening it Roll Back the Transaction.

 

Are you using Form.Show() or Form.ShowDialog() ?

arshad99_0-1686823151852.png

 


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 5 of 10

lbhardwaj4ZUA6
Participant
Participant

Hi @Mohamed_Arshad 

 

Thanks for your response mate. Still not showing any exception message. 

 

Yes I am using Form.ShowDialog().

 

Any other solution? By the way its working successfully with Revit Addin Manager but why is not working as an addin. The setup is correct as other tools are working perfectly fine. 

0 Likes
Message 6 of 10

Mohamed_Arshad
Advisor
Advisor

Can you Please Try the Installer Extracted Addin with Addin-Manager?


Mohamed Arshad K
Software Developer (CAD & BIM)

0 Likes
Message 7 of 10

mhannonQ65N2
Collaborator
Collaborator

Is the Execute method in your command class that opens the form returning Result.Succeeded? If it doesn't, then all your changes will be reverted.

0 Likes
Message 8 of 10

lbhardwaj4ZUA6
Participant
Participant

 

Its there

 

lbhardwaj4ZUA6_1-1686846296884.png

 

0 Likes
Message 9 of 10

mhannonQ65N2
Collaborator
Collaborator

You need to set the form's DialogResult property to DialogResult.OK before closing it to make ShowDialog() return DialogResult.OK.

Alternatively, you could set your OK button's DialogResult property to DialogResult.OK in the designer and remove the click handler from it and clicking it will close the form and return DialogResult.OK.

Message 10 of 10

Mohamed_Arshad
Advisor
Advisor

HI @lbhardwaj4ZUA6 

Kindly Refer the Below Image.

arshad99_0-1686889451831.png


@mhannonQ65N2  But How it will work on Addin- Manager That my Question?

 


Mohamed Arshad K
Software Developer (CAD & BIM)