Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Why my code sometimes works, sometimes doesn't ?

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
bjhuxq
268 Views, 3 Replies

Why my code sometimes works, sometimes doesn't ?

Rule1:

AddReference "Sobektech.InvCustomization.dll"
Imports Sobektech.InvCustomization.Ribbon
Public Class Test
	Public Sub main()
		Dim rb As RuleButton = New RuleButton(thisdoc.document, "Button", "My:Button1", "Show Form")
		rb.Add("Assembly", "id_TabAssemble", "MyPanel")
	End Sub
End Class

dll code:

using Autodesk.iLogic.Automation;
using Inventor;
using System.Collections.Generic;
using System.Linq;

namespace Sobektech.InvCustomization.Ribbon
{

    public class RuleButton
    {
        public static List<RuleButton> RuleButtons = new List<RuleButton>();
        public Inventor.Application InvApp { get; private set; }

        public Inventor.Document Document { get; private set; }

        public string DisplayName { get; set; }

        public string InternalName { get; set; }

        public CommandTypesEnum Classification { get; set; }

        public string ToolTip { get; set; } = "";

        public string Description { get; set; } = "";

        public string RuleName { get; set; }

        public RuleButton(Document doc, string displayName, string internalName, string ruleName,CommandTypesEnum classification=CommandTypesEnum.kNonShapeEditCmdType, string toolTip="", string description="")
        {
            Document = doc;
            InvApp = doc.File.Application;
            DisplayName = displayName;
            InternalName = internalName;
            Classification = classification;
            ToolTip = toolTip;
            Description = description;
            RuleName = ruleName;
            RuleButtons.Add(this);
        }

        public void Add(string ribbon,string tab,string pannel)
        {
            Inventor.ButtonDefinition bd = InvApp.CommandManager.ControlDefinitions.OfType<ButtonDefinition>().FirstOrDefault(b => b.InternalName == InternalName);
            if(bd== null)
            {
                bd = InvApp.CommandManager.ControlDefinitions.AddButtonDefinition(DisplayName, InternalName, Classification, "{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}", DescriptionText:Description,ToolTipText:ToolTip);
                bd.Enabled = true;
                bd.OnExecute += ButtonDefinition_OnExecute;
                InvApp.UserInterfaceManager.Ribbons[ribbon].RibbonTabs[tab].RibbonPanels.Add(pannel, InternalName + pannel, "{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}").CommandControls.AddButton(bd);
            }
            else
            {
                bd.Enabled = true;
                bd.OnExecute += ButtonDefinition_OnExecute;
            }
        }

        public void ButtonDefinition_OnExecute(NameValueMap context)
        {
            System.Windows.Forms.MessageBox.Show("Button Clicked!");
            if (GetiLogicAutomation(InvApp, out iLogicAutomation iLogicAutomation))
                System.Windows.Forms.MessageBox.Show("Got iLogic !");
            iLogicAutomation.RunRule(Document, RuleName);
        }

        public static bool GetiLogicAutomation(Inventor.Application invApp, out Autodesk.iLogic.Automation.iLogicAutomation iLogic)
        {
            ApplicationAddIns addIns = invApp.ApplicationAddIns;
            ApplicationAddIn addIn;
            try
            {
                addIn = addIns.ItemById["{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}"];
                addIn.Activate();
                iLogic = addIn.Automation as iLogicAutomation;
                return true;
            }
            catch
            {
                iLogic = null;
                return false;
            }
        }

    }
}

dll code  using vb.net :

Imports Autodesk.iLogic.Automation
Imports Inventor
Imports System.Collections.Generic
Imports System.Linq
Imports System.Runtime.InteropServices

Namespace Sobektech.InvCustomization.Ribbon

    Public Class RuleButton

        Private _InvApp As Inventor.Application, _Document As Inventor.Document
        Public Shared RuleButtons As List(Of RuleButton) = New List(Of RuleButton)()

        Public Property InvApp As Application
            Get
                Return _InvApp
            End Get
            Private Set(ByVal value As Application)
                _InvApp = value
            End Set
        End Property

        Public Property Document As Document
            Get
                Return _Document
            End Get
            Private Set(ByVal value As Document)
                _Document = value
            End Set
        End Property

        Public Property DisplayName As String

        Public Property InternalName As String

        Public Property Classification As CommandTypesEnum

        Public Property ToolTip As String = ""

        Public Property Description As String = ""

        Public Property RuleName As String

        Public Sub New(ByVal doc As Document, ByVal displayName As String, ByVal internalName As String, ByVal ruleName As String, ByVal Optional classification As CommandTypesEnum = CommandTypesEnum.kNonShapeEditCmdType, ByVal Optional toolTip As String = "", ByVal Optional description As String = "")
            Document = doc
            InvApp = doc.File.Application
            Me.DisplayName = displayName
            Me.InternalName = internalName
            Me.Classification = classification
            Me.ToolTip = toolTip
            Me.Description = description
            Me.RuleName = ruleName
            RuleButtons.Add(Me)
        End Sub

        Public Sub Add(ByVal ribbon As String, ByVal tab As String, ByVal pannel As String)
            Dim bd As ButtonDefinition = InvApp.CommandManager.ControlDefinitions.OfType(Of ButtonDefinition)().FirstOrDefault(Function(b) Equals(b.InternalName, InternalName))
            If bd Is Nothing Then
                bd = InvApp.CommandManager.ControlDefinitions.AddButtonDefinition(DisplayName, InternalName, Classification, "{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}", DescriptionText:=Description, ToolTipText:=ToolTip)
                bd.Enabled = True
                AddHandler bd.OnExecute, AddressOf ButtonDefinition_OnExecute
                InvApp.UserInterfaceManager.Ribbons(ribbon).RibbonTabs(tab).RibbonPanels.Add(pannel, InternalName & pannel, "{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}").CommandControls.AddButton(bd)
            Else
                bd.Enabled = True
                AddHandler bd.OnExecute, AddressOf ButtonDefinition_OnExecute
            End If
        End Sub

        Public Sub ButtonDefinition_OnExecute(ByVal context As NameValueMap)
            Windows.Forms.MessageBox.Show("Button Clicked!")
            Dim iLogicAutomation As iLogicAutomation = Nothing
            If GetiLogicAutomation(InvApp, iLogicAutomation) Then Windows.Forms.MessageBox.Show("Got iLogic !")
            iLogicAutomation.RunRule(Document, RuleName)
        End Sub

        Public Shared Function GetiLogicAutomation(ByVal invApp As Application, <Out> ByRef iLogic As iLogicAutomation) As Boolean
            Dim addIns = invApp.ApplicationAddIns
            Dim addIn As ApplicationAddIn
            Try
                addIn = addIns.ItemById("{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}")
                addIn.Activate()
                iLogic = TryCast(addIn.Automation, iLogicAutomation)
                Return True
            Catch
                iLogic = Nothing
                Return False
            End Try
        End Function

    End Class
End Namespace

 

result:

3 REPLIES 3
Message 2 of 4
Cadkunde.nl
in reply to: bjhuxq

I haven't tried your code.

My feeling, glancing your code, I think it might be because of the Guid.

Instead of a fixed Guid, try:

Guid.NewGuid().ToString()

 

But if you are this far and knowledgable in coding.

I suggest make a real addin which shows the button.

I've never tried it this way, and must be hard debugging.

 

Message 3 of 4
bjhuxq
in reply to: Cadkunde.nl

I can make Addin ribbon button, but this time I want ribbon button appear when others open my .iam file. If I make a Addin, the button will appear everytime.I try to give a new guid ,and it works very well, thanks!

using Autodesk.iLogic.Automation;
using Inventor;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Sobektech.InvCustomization.Ribbon
{

    public class RuleButton
    {
        public static List<RuleButton> RuleButtons = new List<RuleButton>();
        public Inventor.Application InvApp { get; private set; }

        public Inventor.Document Document { get; private set; }

        public string DisplayName { get; set; }

        public string InternalName { get; set; }

        public CommandTypesEnum Classification { get; set; }

        public string ToolTip { get; set; } = "";

        public string Description { get; set; } = "";

        public string RuleName { get; set; }

        private RibbonPanel _panel;

        private Inventor.ButtonDefinition _bd;

        public RuleButton(Document doc, string displayName, string internalName, string ruleName,CommandTypesEnum classification=CommandTypesEnum.kNonShapeEditCmdType, string toolTip="", string description="")
        {
            Document = doc;
            InvApp = doc.File.Application;
            DisplayName = displayName;
            InternalName = internalName;
            Classification = classification;
            ToolTip = toolTip;
            Description = description;
            RuleName = ruleName;
            RuleButtons.Add(this);
            doc.DocumentEvents.OnClose += DocumentEvents_OnClose;
        }

        private void DocumentEvents_OnClose(EventTimingEnum BeforeOrAfter, NameValueMap Context, out HandlingCodeEnum HandlingCode)
        {
            _panel.Delete();
            _bd.Delete();
            HandlingCode=HandlingCodeEnum.kEventHandled;
        }

        public void Add(string ribbon,string tab,string pannel)
        {
            Inventor.ButtonDefinition bd = InvApp.CommandManager.ControlDefinitions.OfType<ButtonDefinition>().FirstOrDefault(b => b.InternalName == InternalName);
            if(bd== null)
            {
                string classId = "{" + Guid.NewGuid().ToString() + "}";
                bd = InvApp.CommandManager.ControlDefinitions.AddButtonDefinition(DisplayName, InternalName, Classification, classId, DescriptionText:Description,ToolTipText:ToolTip);
                bd.Enabled = true;
                bd.OnExecute += ButtonDefinition_OnExecute;
                _panel = InvApp.UserInterfaceManager.Ribbons[ribbon].RibbonTabs[tab].RibbonPanels.Add(pannel, InternalName + pannel, classId);
                _panel.CommandControls.AddButton(bd);
            }
            else
            {
                bd.Enabled = true;
                bd.OnExecute += ButtonDefinition_OnExecute;
            }
            _bd = bd;
        }

        public void ButtonDefinition_OnExecute(NameValueMap context)
        {
            System.Windows.Forms.MessageBox.Show("Button Clicked!");
            if (GetiLogicAutomation(InvApp, out iLogicAutomation iLogicAutomation))
                System.Windows.Forms.MessageBox.Show("Got iLogic !");
            iLogicAutomation.RunRule(Document, RuleName);
        }

        

        public static bool GetiLogicAutomation(Inventor.Application invApp, out Autodesk.iLogic.Automation.iLogicAutomation iLogic)
        {
            ApplicationAddIns addIns = invApp.ApplicationAddIns;
            ApplicationAddIn addIn;
            try
            {
                addIn = addIns.ItemById["{3bdd8d79-2179-4b11-8a5a-257b1c0263ac}"];
                addIn.Activate();
                iLogic = addIn.Automation as iLogicAutomation;
                return true;
            }
            catch
            {
                iLogic = null;
                return false;
            }
        }

    }
}

 

Message 4 of 4
Cadkunde.nl
in reply to: bjhuxq

I think I would still make an addin, display the button.

But let the button do nothing unless some hidden check, like you add a custom iproperty "SebekTech" = True

 

Debugging is better that way, but also the button now does not dissappear when switching, so the problem still exists of misuse.

 

With an addin you can even use the inventor event on document chance, check if custom iproperty exist and show button and hide if not

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report