Inventor Customization

Reply
Valued Mentor
JohanLarsson
Posts: 425
Registered: ‎06-14-2005
Message 1 of 7 (325 Views)
Accepted Solution

DriveConstraintSettings.PlayForward does not animate nor detect contact

325 Views, 6 Replies
10-31-2011 01:55 PM

I played around with the drive constraint and detect contact. It looks really nice and have potential to be really useful.

I wrote a small snippet to try iot from code, but ran into some problems.

 Public Sub driveConstraint()
'    Dim testDoc As AssemblyDocument
    
    Set testDoc = ThisApplication.ActiveDocument
    Dim constraint As AssemblyConstraint
    For Each constraint In testDoc.ComponentDefinition.Constraints
        If constraint.DriveConstraintSettings.IsInitialized Then
            Exit For
        End If
    Next

    constraint.DriveConstraintSettings.GoToStart
    constraint.DriveConstraintSettings.PlayForward
End Sub

 


I see two things that differ from when I run the constraint from code compared to when I do the same via inventor GUI.
PlayForward moves the constraint to the end in one step.
Neither collision detection nor contact detection works.
What have i done wrong?

-------------------------------------------------------------------------
Inventor Professional 2012 SP1 (25 seats with subscription)
Windows 7 64 bit
Lenovo D20, 12 GB RAM, Intel Xeon X5687 3.6 GHz, SSD
Quadro 4000, driver 8.17.12.9573, dual monitors
SpacePilot, driver version 6.15.3 Firmware 3.12
Valued Mentor
JohanLarsson
Posts: 425
Registered: ‎06-14-2005
Message 2 of 7 (321 Views)

Re: DriveConstraintSettings.PlayForward does not animate nor detect contact

10-31-2011 02:37 PM in reply to: JohanLarsson

Inventor 2012 64 Installed SP1, still does not work.

 

Added some more nonsense code but it does not animate.

 Public Sub driveConstraint()
    Dim testDoc As AssemblyDocument
    
    Set testDoc = ThisApplication.ActiveDocument
    Dim constraint As AssemblyConstraint
    For Each constraint In testDoc.ComponentDefinition.Constraints
        If constraint.DriveConstraintSettings.IsInitialized Then
            Exit For
        End If
    Next

    Call constraint.DriveConstraintSettings.GoToStart
    Call constraint.DriveConstraintSettings.PlayForward
    Call constraint.DriveConstraintSettings.PlayReverse
    Call constraint.DriveConstraintSettings.StepForward
    Call constraint.DriveConstraintSettings.StepReverse
End Sub

 

-------------------------------------------------------------------------
Inventor Professional 2012 SP1 (25 seats with subscription)
Windows 7 64 bit
Lenovo D20, 12 GB RAM, Intel Xeon X5687 3.6 GHz, SSD
Quadro 4000, driver 8.17.12.9573, dual monitors
SpacePilot, driver version 6.15.3 Firmware 3.12
Valued Mentor
JohanLarsson
Posts: 425
Registered: ‎06-14-2005
Message 3 of 7 (320 Views)

Re: DriveConstraintSettings.PlayForward does not animate nor detect contact

10-31-2011 03:01 PM in reply to: JohanLarsson

I'm on to something, it seems Inventor does not like the startvalue to be greater than the endvalue. Gonna play around som more and see if I can isolate it.

-------------------------------------------------------------------------
Inventor Professional 2012 SP1 (25 seats with subscription)
Windows 7 64 bit
Lenovo D20, 12 GB RAM, Intel Xeon X5687 3.6 GHz, SSD
Quadro 4000, driver 8.17.12.9573, dual monitors
SpacePilot, driver version 6.15.3 Firmware 3.12
Valued Mentor
JohanLarsson
Posts: 425
Registered: ‎06-14-2005
Message 4 of 7 (313 Views)

Re: DriveConstraintSettings.PlayForward does not animate nor detect contact

11-01-2011 06:03 AM in reply to: JohanLarsson

Now it kinda works, Inventor does not sem to like the StartValue to be less than the EndValue.

 

The following code animates properly.

Public Sub MeasureByContact()
    Dim assyDoc As AssemblyDocument
    Set assyDoc = ThisApplication.ActiveDocument
    Dim oConstraint As AssemblyConstraint
    For Each oConstraint In assyDoc.ComponentDefinition.Constraints
        If oConstraint.DriveConstraintSettings.IsInitialized Then
            With oConstraint.DriveConstraintSettings
                .StartValue = 0
                .EndValue = 20
                .GoToStart
                
                .PlayForward

                .StartValue = -20
                .EndValue = 0
                .GoToEnd
                
                .PlayReverse

            End With
        End If
    Next
End Sub

 One thing remains, to stop at contact. The document is setup to detect contact and when I drive the constraint manually it stops perfectly on contact. When I run it from code it does not stop. Gonna dive into it, please post suggestions if you guys have any.

-------------------------------------------------------------------------
Inventor Professional 2012 SP1 (25 seats with subscription)
Windows 7 64 bit
Lenovo D20, 12 GB RAM, Intel Xeon X5687 3.6 GHz, SSD
Quadro 4000, driver 8.17.12.9573, dual monitors
SpacePilot, driver version 6.15.3 Firmware 3.12
Valued Mentor
JohanLarsson
Posts: 425
Registered: ‎06-14-2005
Message 5 of 7 (312 Views)

Re: DriveConstraintSettings.PlayForward does not animate nor detect contact

11-01-2011 07:06 AM in reply to: JohanLarsson

CollisionDetection does not seem to have the same meaning from the API as from the GUI setting it to true gives me the behaviour I want.

 

The "Activate Contact Solver" seems to be toggled automatically.

 

One word of caution, if the stepsize is to large the contact can fail to be detected.

 

 

Public Sub MeasureByContact()
    Dim assyDoc As AssemblyDocument
    Set assyDoc = ThisApplication.ActiveDocument
    Dim oConstraint As AssemblyConstraint
    assyDoc.ModelingSettings.InteractiveContactAnalysis = kAllComponentsInteractiveContact
    assyDoc.ModelingSettings.InteractiveContactSurfaces = kAllSurfacesInteractiveContact

    
    For Each oConstraint In assyDoc.ComponentDefinition.Constraints
        If oConstraint.DriveConstraintSettings.IsInitialized Then
            With oConstraint.DriveConstraintSettings
                .CollisionDetection = True
                .StartValue = 0
                .EndValue = 20
                .GoToStart
                
                .PlayForward

                .StartValue = -20
                .EndValue = 0
                .GoToEnd
                
                .PlayReverse

            End With
        End If
    Next
End Sub

 

-------------------------------------------------------------------------
Inventor Professional 2012 SP1 (25 seats with subscription)
Windows 7 64 bit
Lenovo D20, 12 GB RAM, Intel Xeon X5687 3.6 GHz, SSD
Quadro 4000, driver 8.17.12.9573, dual monitors
SpacePilot, driver version 6.15.3 Firmware 3.12
Valued Mentor
JohanLarsson
Posts: 425
Registered: ‎06-14-2005
Message 6 of 7 (301 Views)

Re: DriveConstraintSettings.PlayForward does not animate nor detect contact

11-02-2011 07:07 AM in reply to: JohanLarsson

The corresponding code from .Net:

Caution, this is just a quick hack and will not work for all types of constraints. The unitsconversion is hardcoded to convert to Inventor.UnitsTypeEnum.kDatabaseLengthUnits

Probably numerous bugs, but they are free:smileywink:

 

        /// <summary>
        ///A test for DriveConstraint
        ///</summary>
        [TestMethod()]
        public void DriveConstraint()
        {
            Inventor.AssemblyDocument assemblyDocument = GetActiveAssemblyDocument();
            assemblyDocument.ModelingSettings.InteractiveContactAnalysis =
                Inventor.InteractiveContactAnalysisEnum.kAllComponentsInteractiveContact;
            assemblyDocument.ModelingSettings.InteractiveContactSurfaces =
                Inventor.InteractiveContactSurfacesEnum.kAllSurfacesInteractiveContact;

            foreach (Inventor.AssemblyConstraint constraint in assemblyDocument.ComponentDefinition.Constraints)
            {
                if(constraint.DriveConstraintSettings.IsInitialized)
                {
                    constraint.DriveConstraintSettings.CollisionDetection = true;
                    constraint.DriveConstraintSettings.SetIncrement(Inventor.ConstraintIncrementTypeEnum.kIncrementAsAmountOfValue, GetExpressionString(assemblyDocument,2,Inventor.UnitsTypeEnum.kMillimeterLengthUnits));
                    constraint.DriveConstraintSettings.StartValue = GetExpressionString(assemblyDocument, 0,
                                                                                        Inventor.UnitsTypeEnum.
                                                                                            kMillimeterLengthUnits);


                    constraint.DriveConstraintSettings.EndValue = GetExpressionString(assemblyDocument, 20,
                                                                                      Inventor.UnitsTypeEnum.
                                                                                          kMillimeterLengthUnits);
                    constraint.DriveConstraintSettings.GoToStart();
                    constraint.DriveConstraintSettings.PlayForward();

                    constraint.DriveConstraintSettings.StartValue = GetExpressionString(assemblyDocument, -20,
                                                                                        Inventor.UnitsTypeEnum.
                                                                                            kMillimeterLengthUnits);


                    constraint.DriveConstraintSettings.EndValue = GetExpressionString(assemblyDocument, 0,
                                                                                      Inventor.UnitsTypeEnum.
                                                                                          kMillimeterLengthUnits);
                    constraint.DriveConstraintSettings.GoToEnd();
                    constraint.DriveConstraintSettings.PlayReverse();
                }
            }
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        private string GetExpressionString(Inventor.AssemblyDocument assemblyDocument, double value, Inventor.UnitsTypeEnum unitsTypeEnum)
        {
            double convertedValue = assemblyDocument.UnitsOfMeasure.ConvertUnits(value, unitsTypeEnum,
                                                                                 Inventor.UnitsTypeEnum.
                                                                                     kDatabaseLengthUnits);
            string expressionString=assemblyDocument.UnitsOfMeasure.GetStringFromValue(convertedValue,
                                                   Inventor.UnitsTypeEnum.kMillimeterLengthUnits);
            return expressionString;

        }

        public static Inventor.AssemblyDocument GetActiveAssemblyDocument()
        {
            if (!(InventorUtilities.GetInvetorActiveDocument() is AssemblyDocument))
            {
                throw new InstanceNotFoundException("No active asseblydocument");
            }
            Inventor.AssemblyDocument assemblyDocument = InventorUtilities.GetInvetorActiveDocument() as AssemblyDocument;
            if (assemblyDocument == null) throw new InstanceNotFoundException("No active asseblydocument");
            return assemblyDocument;
        }

 

-------------------------------------------------------------------------
Inventor Professional 2012 SP1 (25 seats with subscription)
Windows 7 64 bit
Lenovo D20, 12 GB RAM, Intel Xeon X5687 3.6 GHz, SSD
Quadro 4000, driver 8.17.12.9573, dual monitors
SpacePilot, driver version 6.15.3 Firmware 3.12
Valued Mentor
JohanLarsson
Posts: 425
Registered: ‎06-14-2005
Message 7 of 7 (297 Views)

Re: DriveConstraintSettings.PlayForward does not animate nor detect contact

11-02-2011 10:42 AM in reply to: JohanLarsson

Refactored it a bit nicer:

    public static class AssemblyConstraintUtilities
    {
        public static void DriveConstraint(Inventor.AssemblyConstraint assemblyConstraint, double fromPosition, double toPosition, Inventor.UnitsTypeEnum unitsTypeEnum, bool analyzeContact)
        {
            if (assemblyConstraint == null) throw new ArgumentNullException("assemblyConstraint");
            if (Math.Abs(fromPosition - toPosition) < double.Epsilon) throw new InvalidOperationException("Startvalue == Endvalue");
            Inventor.AssemblyDocument assemblyDocument = assemblyConstraint.Parent.Document;
            Inventor.UnitsOfMeasure unitsOfMeasure = assemblyDocument.UnitsOfMeasure;
            assemblyDocument.ModelingSettings.InteractiveContactAnalysis = Inventor.InteractiveContactAnalysisEnum.kAllComponentsInteractiveContact;
            assemblyDocument.ModelingSettings.InteractiveContactSurfaces = Inventor.InteractiveContactSurfacesEnum.kAllSurfacesInteractiveContact;

            Inventor.UnitsTypeEnum dataBaseUnitEnum = GetDataBaseUnitEnum(assemblyConstraint);

            assemblyConstraint.DriveConstraintSettings.CollisionDetection = analyzeContact;

            string incrementExpressionString = InventorUtilities.GetExpressionString(unitsOfMeasure, 1, unitsTypeEnum, dataBaseUnitEnum);

            assemblyConstraint.DriveConstraintSettings.SetIncrement(Inventor.ConstraintIncrementTypeEnum.kIncrementAsAmountOfValue, incrementExpressionString);

            string fromExpressionString = InventorUtilities.GetExpressionString(unitsOfMeasure, fromPosition, unitsTypeEnum, dataBaseUnitEnum);

            string toExpressionString = InventorUtilities.GetExpressionString(unitsOfMeasure, toPosition, unitsTypeEnum, dataBaseUnitEnum);

            if (fromPosition > toPosition)
            {
                DriveConstraintInNegativeDirection(assemblyConstraint, fromExpressionString, toExpressionString);
            }
            else if (fromPosition < toPosition)
            {
                DriveConstraintInPositiveDirection(assemblyConstraint, fromExpressionString, toExpressionString);
            }
        }

        private static void DriveConstraintInNegativeDirection(AssemblyConstraint assemblyConstraint, string fromExpressionString, string toExpressionString)
        {
            assemblyConstraint.DriveConstraintSettings.StartValue = toExpressionString;
            assemblyConstraint.DriveConstraintSettings.EndValue = fromExpressionString;
            assemblyConstraint.DriveConstraintSettings.GoToEnd();
            assemblyConstraint.DriveConstraintSettings.PlayReverse();
        }

        private static void DriveConstraintInPositiveDirection(AssemblyConstraint assemblyConstraint, string fromExpressionString, string toExpressionString)
        {
            assemblyConstraint.DriveConstraintSettings.StartValue = fromExpressionString;
            assemblyConstraint.DriveConstraintSettings.EndValue = toExpressionString;
            assemblyConstraint.DriveConstraintSettings.GoToStart();
            assemblyConstraint.DriveConstraintSettings.PlayForward();
        }

        private static UnitsTypeEnum GetDataBaseUnitEnum(AssemblyConstraint assemblyConstraint)
        {
            Inventor.UnitsTypeEnum dataBaseUnitEnum;
            if ((assemblyConstraint is Inventor.MateConstraint) || (assemblyConstraint is Inventor.FlushConstraint) ||
                (assemblyConstraint is Inventor.InsertConstraint) || (assemblyConstraint is Inventor.TangentConstraint))
            {
                dataBaseUnitEnum = Inventor.UnitsTypeEnum.kDatabaseLengthUnits;
            }
            else if (assemblyConstraint is Inventor.AngleConstraint)
            {
                dataBaseUnitEnum = Inventor.UnitsTypeEnum.kDatabaseAngleUnits;
            }
            else
            {
                throw new InstanceNotFoundException("Unknown constraint type");
            }
            return dataBaseUnitEnum;
        }
    }

 

-------------------------------------------------------------------------
Inventor Professional 2012 SP1 (25 seats with subscription)
Windows 7 64 bit
Lenovo D20, 12 GB RAM, Intel Xeon X5687 3.6 GHz, SSD
Quadro 4000, driver 8.17.12.9573, dual monitors
SpacePilot, driver version 6.15.3 Firmware 3.12

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community


Inventor Exchange Apps

Created by the community for the community, Autodesk Exchange Apps for Autodesk Inventor helps you achieve greater speed, accuracy, and automation from concept to manufacturing.

Connect with Inventor

Twitter

Facebook

Blogs

Pinterest

Youtube