Auto select face of washer to constraint when placed in an assembly (similar to content center)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello everyone,
I'm trying to place a set of fasteners into assembly using ilogic code. I'm not a programmer but can understand codes and write basic ones.
WHAT THE CODE DOES:
With the inputs from this forum I have written a code that places a set of fasteners (bolt, flat washer and spring washer) from content center and constraint them together and then constarint them to a hole by picking faces with the mouse selection.
The code can also create a pattern if the hole it is constrained to is part of a feature pattern.
WHAT I NEED TO ADD:
1. I need the code to auto select the faces of the washer and only place the washer after selecting the faces of the hole just like content center works with individual parts, I need to achieve it with a set.
2. At the least, if the above is complicated, I need the parts to be placed where I click my mouse so that I dont have to find them in an assembly.
Any help is much appreciated. Thank for all the help I have already received from this forum...
oApp = ThisApplication Dim oDoc As Document = oApp.ActiveDocument Dim oCompDef As AssemblyComponentDefinition = oDoc.ComponentDefinition Dim oAllOcc As ComponentOccurrences Dim oOcc As ComponentOccurrence Dim oCompDef1 As ComponentDefinition Dim oCompDef2 As ComponentDefinition Dim oPart1 As ComponentOccurrence Dim oPart2 As ComponentOccurrence 'Adding and Constraining the fasteners... 'It is important to leave the browser name blank "" so that new instances are named in increments. 'Also instead Of mentioning the browser name of the Constraint while adding the Constraint mention it As Component.Name, 'so that a New Constraint Is created For every instance... SelectSet = InputListBox("Select Fastener set", MultiValue.List("SelectSet"), SelectSet, Title := "Select Set") If SelectSet = "" Then Exit Sub Select Case SelectSet Case "Xtie" While True XtieBoltSize = InputListBox("Select M10 Bolt Size", MultiValue.List("XtieBoltSize"), XtieBoltSize, Title := "XtieBolts") If XtieBoltSize = "" Then Exit While Components.ContentCenterLanguage = "en-US" Dim Component1 = Components.AddContentCenterPart("", "Fasteners:Washers:Plain", "SS Flat Washers", {"STOCK_TYPE","S","NND","10"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Dim Component2 = Components.AddContentCenterPart("","Fasteners:Washers:Spring","SS Spring Washers", {"STOCK_TYPE","S","NND","10"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Constraints.AddMate("", Component1.Name, "Work Axis1", Component2.Name, "Work Axis1", offset := 0.0, e1InferredType := InferredTypeEnum.kNoInference, e2InferredType := InferredTypeEnum.kNoInference, solutionType := MateConstraintSolutionTypeEnum.kNoSolutionType, biasPoint1 := Nothing, biasPoint2 := Nothing) Constraints.AddMate("", Component1.Name, "YZ PLANE", Component2.Name, "YZ PLANE", offset := 0.0, e1InferredType := InferredTypeEnum.kNoInference, e2InferredType := InferredTypeEnum.kNoInference, solutionType := MateConstraintSolutionTypeEnum.kNoSolutionType, biasPoint1 := Nothing, biasPoint2 := Nothing) Select Case XtieBoltSize Case "M10x25" Dim Component3 = Components.AddContentCenterPart("", "Fasteners:Bolts:Hex Head", "SS HEX HEAD SET SCREWS", {"STOCK_TYPE","S","SIZE_SEL","M10","NLG","25"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Constraints.AddMate("", Component3.Name, "X Axis", Component2.Name, "X Axis", solutionType := MateConstraintSolutionTypeEnum.kOpposedSolutionType) Constraints.AddMate("", Component3.Name, "YZ Plane", Component2.Name, "YZ Plane", 2.35 mm) Case "M10x30" Dim Component4 = Components.AddContentCenterPart("", "Fasteners:Bolts:Hex Head", "SS HEX HEAD SET SCREWS", {"STOCK_TYPE","S","SIZE_SEL","M10","NLG","30"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Constraints.AddMate("", Component4.Name, "X Axis", Component2.Name, "X Axis", solutionType := MateConstraintSolutionTypeEnum.kOpposedSolutionType) Constraints.AddMate("", Component4.Name, "YZ Plane", Component2.Name, "YZ Plane", 2.35 mm) Case "M10x35" Dim Component5 = Components.AddContentCenterPart("", "Fasteners:Bolts:Hex Head", "SS HEX HEAD SET SCREWS", {"STOCK_TYPE","S","SIZE_SEL","M10","NLG","35"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Constraints.AddMate("", Component5.Name, "X Axis", Component2.Name, "X Axis", solutionType := MateConstraintSolutionTypeEnum.kOpposedSolutionType) Constraints.AddMate("", Component5.Name, "YZ Plane", Component2.Name, "YZ Plane", 2.35 mm) Case "M10x40" Dim Component6 = Components.AddContentCenterPart("", "Fasteners:Bolts:Hex Head", "SS HEX HEAD SET SCREWS", {"STOCK_TYPE","S","SIZE_SEL","M10","NLG","40"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Constraints.AddMate("", Component6.Name, "X Axis", Component2.Name, "X Axis", solutionType := MateConstraintSolutionTypeEnum.kOpposedSolutionType) Constraints.AddMate("", Component6.Name, "YZ Plane", Component2.Name, "YZ Plane", 2.35 mm) Case "M10x45" Dim Component7 = Components.AddContentCenterPart("", "Fasteners:Bolts:Hex Head", "SS HEX HEAD SET SCREWS", {"STOCK_TYPE","NS","SIZE_SEL","M10","NLG","45"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Constraints.AddMate("", Component7.Name, "X Axis", Component2.Name, "X Axis", solutionType := MateConstraintSolutionTypeEnum.kOpposedSolutionType) Constraints.AddMate("", Component7.Name, "YZ Plane", Component2.Name, "YZ Plane", 2.35 mm) End Select oDoc.Update ' End While Case "RR" While True RRBoltSize = InputListBox("Select M8 Bolt Size", MultiValue.List("RRBoltSize"), RRBoltSize, Title := "Title", ListName := "List") If RRBoltSize = "" Then Exit While Dim Component8 = Components.AddContentCenterPart("", "Fasteners:Washers:Plain", "SS Flat Washers", {"STOCK_TYPE","S","NND","8"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Dim Component9 = Components.AddContentCenterPart("","Fasteners:Washers:Spring","SS Spring Washers", {"STOCK_TYPE","S","NND","8"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Constraints.AddMate("", Component8.Name, "Work Axis1", Component9.Name, "Work Axis1", offset := 0.0, e1InferredType := InferredTypeEnum.kNoInference, e2InferredType := InferredTypeEnum.kNoInference, solutionType := MateConstraintSolutionTypeEnum.kNoSolutionType, biasPoint1 := Nothing, biasPoint2 := Nothing) Constraints.AddMate("", Component8.Name, "YZ PLANE", Component9.Name, "YZ PLANE", offset := 0.0, e1InferredType := InferredTypeEnum.kNoInference, e2InferredType := InferredTypeEnum.kNoInference, solutionType := MateConstraintSolutionTypeEnum.kNoSolutionType, biasPoint1 := Nothing, biasPoint2 := Nothing) Select Case RRBoltSize Case "M8x16" Dim Component10 = Components.AddContentCenterPart("", "Fasteners:Bolts:Hex Head", "SS HEX HEAD SET SCREWS", {"STOCK_TYPE","S","SIZE_SEL","M8","NLG","16"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Constraints.AddMate("", Component10.Name, "X Axis", Component9.Name, "X Axis", solutionType := MateConstraintSolutionTypeEnum.kOpposedSolutionType) Constraints.AddMate("", Component10.Name, "YZ Plane", Component9.Name, "YZ Plane", 2.1 mm) Case "M8x20" Dim Component11 = Components.AddContentCenterPart("", "Fasteners:Bolts:Hex Head", "SS HEX HEAD SET SCREWS", {"STOCK_TYPE","S","SIZE_SEL","M8","NLG","20"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Constraints.AddMate("", Component11.Name, "X Axis", Component9.Name, "X Axis", solutionType := MateConstraintSolutionTypeEnum.kOpposedSolutionType) Constraints.AddMate("", Component11.Name, "YZ Plane", Component9.Name, "YZ Plane", 2.1 mm) Case "M8x25" Dim Component12 = Components.AddContentCenterPart("", "Fasteners:Bolts:Hex Head", "SS HEX HEAD SET SCREWS", {"STOCK_TYPE","S","SIZE_SEL","M8","NLG","25"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Constraints.AddMate("", Component12.Name, "X Axis", Component9.Name, "X Axis", solutionType := MateConstraintSolutionTypeEnum.kOpposedSolutionType) Constraints.AddMate("", Component12.Name, "YZ Plane", Component9.Name, "YZ Plane", 2.1 mm) End Select oDoc.Update ' End While Case "Wstrip" While True WSFasteners = InputListBox("Select M5 SS CSK Size", MultiValue.List("WSFasteners"), WSFasteners, Title := "Title", ListName := "List") If WSFasteners = "" Then Exit While Dim Component13 = Components.AddContentCenterPart("", "Fasteners:Washers:Plain", "SS Flat Washers", {"STOCK_TYPE","S","NND","5"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Dim Component14 = Components.AddContentCenterPart("","Fasteners:Nuts:Flanged & Nyloc","SS Nyloc Nuts", {"STOCK_TYPE","S","SIZE_SEL","5"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Constraints.AddMate("", Component13.Name, "Work Axis1", Component14.Name, "Work Axis1", offset := 0.0, e1InferredType := InferredTypeEnum.kNoInference, e2InferredType := InferredTypeEnum.kNoInference, solutionType := MateConstraintSolutionTypeEnum.kNoSolutionType, biasPoint1 := Nothing, biasPoint2 := Nothing) Constraints.AddMate("", Component13.Name, "YZ PLANE", Component14.Name, "YZ PLANE", offset := 0.0, e1InferredType := InferredTypeEnum.kNoInference, e2InferredType := InferredTypeEnum.kNoInference, solutionType := MateConstraintSolutionTypeEnum.kNoSolutionType, biasPoint1 := Nothing, biasPoint2 := Nothing) Select Case WSFasteners Case "M5x12" Dim Component15 = Components.AddContentCenterPart("", "Fasteners:Bolts:Countersunk", "SS CSK SHCS", {"STOCK_TYPE","S","SIZE_SEL","M5","NLG","12"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Constraints.AddMate("", Component15.Name, "Z Axis", Component14.Name, "X Axis", solutionType := MateConstraintSolutionTypeEnum.kOpposedSolutionType) Constraints.AddMate("", Component15.Name, "XY Plane", Component14.Name, "YZ Plane", -7 mm) Case "M5x16" Dim Component16 = Components.AddContentCenterPart("", "Fasteners:Bolts:Countersunk", "SS CSK SHCS", {"STOCK_TYPE","S","SIZE_SEL","M5","NLG","16"}, position := Nothing, grounded := False, visible := True, appearance := Nothing) Constraints.AddMate("", Component16.Name, "Z Axis", Component14.Name, "X Axis", solutionType := MateConstraintSolutionTypeEnum.kOpposedSolutionType) Constraints.AddMate("", Component16.Name, "XY Plane", Component14.Name, "YZ Plane", -10 mm) End Select oDoc.Update End While End Select Dim oHole1 As Face 'first hole (proxy hole) is the cylindrical face/axis of our fastener... oHole1 = oApp.CommandManager.Pick(SelectionFilterEnum.kPartFaceCylindricalFilter, "Pick Bolt/Washer Cylindrical Face, Press Esc to cancel") If oHole1 Is Nothing Then Exit Sub 'exit rule if esc button is pressed... oPart1 = oHole1.ContainingOccurrence 'our part is the part that contains the hole we selected with the mouse (the fastener)... Dim oFP1 As FaceProxy 'For parts in the assembly we need to define proxy faces as it could any part and a specific part cannot be mentioned... oPart1.CreateGeometryProxy(oHole1, oFP1) 'FP1 is the proxy feature for oHole1... Dim oHole2 As Face oHole2 = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceCylindricalFilter, "Pick Part Hole Face, Press Esc to cancel") If oHole2 Is Nothing Then Exit Sub oPart2 = oHole2.ContainingOccurrence Dim oFP2 As FaceProxy oPart2.CreateGeometryProxy(oHole2, oFP2) Dim oConstraint1 As AssemblyConstraint = oCompDef.Constraints.AddMateConstraint(oFP1, oFP2, "0", InferredTypeEnum.kInferredLine, InferredTypeEnum.kInferredLine) 'Mate constraint between axis... oDoc.Update Dim oFace1 As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Pick Washer Face. Press Esc to cancel") If oFace1 Is Nothing Then Exit Sub Dim oFace2 As Face = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFacePlanarFilter, "Pick Part face. Press Esc to cancel") If oFace2 Is Nothing Then Exit Sub oCompDef.Constraints.AddMateConstraint(oFace1, oFace2, "0") 'Mate constraint between faces... oDoc.Update ' Dim oFeature As PartFeature oFeature = oFP2.CreatedByFeature ' oFeature.Name = oFP2.CreatedByFeature.Name oCompDef2 = oFeature.Parent Dim oPF As PartFeature Dim oPartFeatures As PartFeatures = oCompDef2.Features Dim oFeature1 As PartFeature' oFeature1 is the pattern part feature required Dim oProxyFeature1 As Object 'oProxyFeature1 will be the proxy of oFeature1... Dim oProxyFeature2 As Object 'oProxyFeature2 will be the proxy of oFeature... 'Creating a collection... Dim oTO As TransientObjects = ThisApplication.TransientObjects Dim oComps As ObjectCollection = oTO.CreateObjectCollection Dim oComp As Object Dim Highlight As HighlightSet = oDoc.HighlightSets.Add() Highlight.Color = oTO.CreateColor(0,75,255,0.2) 'If the parent hole/first hole is selected in a pattern... If oFeature.Type = kHoleFeatureProxyObject Or oFeature.Type = kCutFeatureProxyObject Or oFeature.Type = kExtrudeFeatureObject Then For Each oPF In oPartFeatures If oPF.Type = kCircularPatternFeatureObject Then If oFeature.Name = oPF.ParentFeatures.Item(1).Name Then oFeature1 = oPF 'Assigning the pattern feature to a variable. In this case circular pattern... End If End If If oPF.Type = kRectangularPatternFeatureObject Then If oFeature.Name = oPF.ParentFeatures.Item(1).Name Then oFeature1 = oPF 'Assigning the pattern feature to a variable. In this case circular pattern... End If End If Next oCompDef.Occurrences.AllReferencedOccurrences(oCompDef2).Item(1).CreateGeometryProxy(oFeature1, oProxyFeature1) Highlight.AddItem(oProxyFeature1) Dim oPattern As MsgBoxResult = MessageBox.Show("Pattern components?", "", MessageBoxButtons.YesNo) If oPattern = vbYes Then 'Loop for selecting components that need to be added to the collection... While True oComp = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select the Components. Esc to Continue.") If oComp Is Nothing Then Exit While oComps.Add(oComp) Highlight.AddItem(oComp) End While oCompDef.OccurrencePatterns.AddFeatureBasedPattern(oComps, oProxyFeature1) End If Else 'oFeature is created by a pattern in the part. So, ofeature id already the feature pattern... Dim oFirstHole As PartFeature = oFeature.ParentFeatures.Item(1) Dim oFirstFace As FaceProxy = oFirstHole.Faces.Item(1) oConstraint1.Delete Dim oConstraint2 As AssemblyConstraint = oCompDef.Constraints.AddMateConstraint(oFP1, oFirstFace, "0", InferredTypeEnum.kInferredLine, InferredTypeEnum.kInferredLine) oDoc.Update oCompDef.Occurrences.AllReferencedOccurrences(oCompDef2).Item(1).CreateGeometryProxy(oFeature, oProxyFeature2) Highlight.AddItem(oProxyFeature2) Dim oPattern As MsgBoxResult = MessageBox.Show("Pattern components?", "",MessageBoxButtons.YesNo) If oPattern = vbYes Then 'Loop for selecting components that need to be added to the collection... While True oComp = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyOccurrenceFilter, "Select the Components. Esc to Continue.") If oComp Is Nothing Then Exit While oComps.Add(oComp) Highlight.AddItem(oComp) End While oCompDef.OccurrencePatterns.AddFeatureBasedPattern(oComps, oProxyFeature2) Else If oPattern = vbNo Then oConstraint2.Delete oConstraint1 = oCompDef.Constraints.AddMateConstraint(oFP1, oFP2, "0", InferredTypeEnum.kInferredLine, InferredTypeEnum.kInferredLine) End If End If