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?
Solved! Go to Solution.
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
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.
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.
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
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
/// <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; }
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; } }
Can't find what you're looking for? Ask the community or share your knowledge.