Hello Everyone, Greetings ๐
I have created an iLogic code for my part which automatically created dimension when a rule is run for diemnsions creation using iLogic.
I have Inventor 2019 so I am using "Assign Name" functionality (similar to attribute manager add-in but much simpler) to create these dimensions.
Now each time the part is updated the dimension will be created between the two faces of the part, which is what I want.
Now I also want to delete the previously created iLogic dimensions because after each update o the part, previous dimension is staying their and new dimension is being added over it which obliviously i dont want.
I found this code in search of my answer but when I run this code, nothing happens whatsoever, not even an error is returned. So can someone please help me understand what is it I should do accomplish what I want?
Doc = ThisApplication.ActiveDocument Dim Sheet As Inventor.Sheet oSheet = Doc.ActiveSheet 'Iterate over all diemnsions Dim oDim As GeneralDimension For Each oDim In oSheet.DrawingDimensions If oDim.AttributeSets.NameIsUsed("iLogic_Created") = True Then oDim.Delete End If Next Dim oDoc As Inventor.Document oDoc = ThisApplication.ActiveDocument
Here is the code I am using to generate dimensions for the part, which is working as it should, in case someone needs it ๐
Dim partDoc As PartDocument = ThisDoc.ModelDocument 'assinging variablr to active part documemnt Dim namedEntities = iLogicVb.Automation.GetNamedEntities(partDoc) 'calling in all named faces in the active part doc face0 = namedEntities.FindEntity("J_Face") 'assign named faces face1 = namedEntities.FindEntity("J_end") Dim oSheet = ThisApplication.ActiveDocument.ActiveSheet 'Assigning Variable to active sheet Dim oView As DrawingView = ActiveSheet.View("VIEW1").View 'assign variable to active sheet 'Dim curve0 As DrawingCurvesEnumerator 'no idea why curve0 = oView.DrawingCurves(face0) 'assign faces to variable as drawing curves curve1 = oView.DrawingCurves(face1) Dim finalCurve As DrawingCurve 'assigning a variable as drawing curve to store faces values Dim finalCurve1 As DrawingCurve 'MessageBox.Show(curve1.Count.ToString) finalCurve = curve0.Item(1) 'assigning curve faces values to variables finalCurve1 = curve1.Item(1) 'MessageBox.Show(finalCurve1.ToString) Dim oGeomIntent1 As GeometryIntent 'assigning a variable to geometry intent to use as annotations Dim oGeomIntent2 As GeometryIntent Dim oGeneralDims As GeneralDimensions = oSheet.DrawingDimensions.GeneralDimensions ' gaining acces to dimensioning in sheet 'oGeomIntent1 = oSheet.CreateGeometryIntent(finalCurve) 'oGeomIntent2 = oSheet.CreateGeometryIntent(finalCurve.EndPoint) oGeomIntent1 = oSheet.CreateGeometryIntent(finalCurve) ' assigning drawing curve values to geometry variable oGeomIntent2 = oSheet.CreateGeometryIntent(finalCurve1) Dim textPoint1 As Inventor.Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oView.Center.X - 6.5 , oView.Top) 'Dim textPoint2 As Inventor.Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oView.Center.X, oView.Top + .10) oDim = oGeneralDims.AddLinear(textPoint1, oGeomIntent1, oGeomIntent2) 'oDim = oGeneralDims.AddLinear(textPoint2, oGeomIntent2) 'Dim linDim1 = genDims.AddLinear("Dimension 1", VIEW1.SheetPoint(0.5, -0.1),face1,Face2) Comapring code
Solved! Go to Solution.
Solved by JhoelForshav. Go to Solution.
Hi @sohaib.as01
In order to make the code to delete dimensions work, you'll need to add the attribute set "iLogic_Created" to the dimensions you create with your code. See code below, I just added the one line ๐
Dim partDoc As PartDocument = ThisDoc.ModelDocument 'assinging variablr to active part documemnt Dim namedEntities = iLogicVb.Automation.GetNamedEntities(partDoc) 'calling in all named faces in the active part doc face0 = namedEntities.FindEntity("J_Face") 'assign named faces face1 = namedEntities.FindEntity("J_end") Dim oSheet = ThisApplication.ActiveDocument.ActiveSheet 'Assigning Variable to active sheet Dim oView As DrawingView = ActiveSheet.View("VIEW1").View 'assign variable to active sheet 'Dim curve0 As DrawingCurvesEnumerator 'no idea why curve0 = oView.DrawingCurves(face0) 'assign faces to variable as drawing curves curve1 = oView.DrawingCurves(face1) Dim finalCurve As DrawingCurve 'assigning a variable as drawing curve to store faces values Dim finalCurve1 As DrawingCurve 'MessageBox.Show(curve1.Count.ToString) finalCurve = curve0.Item(1) 'assigning curve faces values to variables finalCurve1 = curve1.Item(1) 'MessageBox.Show(finalCurve1.ToString) Dim oGeomIntent1 As GeometryIntent 'assigning a variable to geometry intent to use as annotations Dim oGeomIntent2 As GeometryIntent Dim oGeneralDims As GeneralDimensions = oSheet.DrawingDimensions.GeneralDimensions ' gaining acces to dimensioning in sheet 'oGeomIntent1 = oSheet.CreateGeometryIntent(finalCurve) 'oGeomIntent2 = oSheet.CreateGeometryIntent(finalCurve.EndPoint) oGeomIntent1 = oSheet.CreateGeometryIntent(finalCurve) ' assigning drawing curve values to geometry variable oGeomIntent2 = oSheet.CreateGeometryIntent(finalCurve1) Dim textPoint1 As Inventor.Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oView.Center.X - 6.5 , oView.Top) 'Dim textPoint2 As Inventor.Point2d = ThisApplication.TransientGeometry.CreatePoint2d(oView.Center.X, oView.Top + .10) oDim = oGeneralDims.AddLinear(textPoint1, oGeomIntent1, oGeomIntent2) 'Just add the attribute set to the dimension oDim.AttributeSets.Add("iLogic_Created") 'oDim = oGeneralDims.AddLinear(textPoint2, oGeomIntent2) 'Dim linDim1 = genDims.AddLinear("Dimension 1", VIEW1.SheetPoint(0.5, -0.1),face1,Face2) Comapring code
If you run this rule and then run the rule to delete dimensions you posted It'll work ๐
Doc = ThisApplication.ActiveDocument Dim Sheet As Inventor.Sheet oSheet = Doc.ActiveSheet 'Iterate over all diemnsions Dim oDim As GeneralDimension For Each oDim In oSheet.DrawingDimensions If oDim.AttributeSets.NameIsUsed("iLogic_Created") = True Then oDim.Delete End If Next
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
Hey thank you so much @JhoelForshav your solution worked like a charm :)) I am a Mechanical Engineer, so not very good at programming, just remember stuff what i learned in University ๐
Can you also solve another final problem for me if you got time?
Now I want the same code to run for assembly, so I figured out this code from internet help and hit & trial. But its returning me an error "Object reference not set to an instance of an object".
I ran a Msg Box check to see which line is giving me this error; hence found out the line below
MessageBox.Show(check)
is giving me the error. So I suppose I am not calling the assembly part correctly? Also please note that I want a dimension between two same parts spaced apart , but for now I am just trying to get a dimension of just one face of the part to see if my code works correctly. Here is the code I am trying to run;
check = 2 Dim Doc As DrawingDocument Doc = ThisDoc.Document Dim oAsmDoc As AssemblyDocument Dim oCompDef As AssemblyComponentDefinition'assinging variable for component definations i assy Dim FPColl As ObjectCollection Dim OverallColl As ObjectCollection Dim oText As Point2d MessageBox.Show(check) Dim S1 = oCompDef.Occurrences.ItemByName("J channel LED edge:1") 'Dim S2 = oCompDef.Occurrences.ItemByName("J Channel LED edge:2") FPColl = ThisServer.TransientObjects.CreateObjectCollection OverallColl = ThisServer.TransientObjects.CreateObjectCollection Dim namedEntities = iLogicVb.Automation.GetNamedEntities(S1.Definition.Document)'calling in all named faces in the active part doc 'Dim namedEntities = iLogicVb.Automation.GetNamedEntities(S2.Definition.Document) S1Face = namedEntities.FindEntity("J_Face") 'assign named faces 'face1 = namedEntities.FindEntity("J_end") Dim oSheet = ThisApplication.ActiveDocument.ActiveSheet 'Assigning Variable to active sheet Dim oView As DrawingView = ActiveSheet.View("FRONT").View 'assign variable to active sheet Call S1.CreateGeometryProxy(S1Face, oSketchProxy) Dim S1Curve = oView.DrawingCurves(oSketchProxy) oIntent = oSheet.CreateGeometryIntent(S1Curve.Item(1)) Call OverallColl.Add(oIntent) oText = ThisServer.TransientGeometry.CreatePoint2d(oView.Center.X - oView.Width / 2, oView.Center.Y - oView.Height / 2) oText.Y = oText.Y - 1 Call oSheet.DrawingDimensions.GeneralDimensions.AddLinear(oText, OverallColl.Item(1)).CenterText
You're right that you never set a reference to the AssemblyDocument. The variables oAsmDoc and oCompDef are empty.
I think I understand from your code what you're trying to accomplish though so I wrote the code below for you with comments explaining all the operations. Hope it helps ๐
'Get the drawing Dim oDrawing As DrawingDocument = ThisDrawing.Document 'Get the active sheet Dim oSheet As Sheet = oDrawing.ActiveSheet 'Get the view named "FRONT" Dim oView As DrawingView For Each drwView As DrawingView In oSheet.DrawingViews If drwView.Name = "FRONT" oView = drwView Exit For End If Next 'Exit if view can't be found If oView Is Nothing MsgBox("Couldn't find the drawing view named FRONT") Exit Sub End If 'Get the assembly document from the view Dim oAsm As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument Dim oCompDef As AssemblyComponentDefinition = oAsm.ComponentDefinition Dim oOcc As ComponentOccurrence 'Get the occurrence named J channel LED edge:1 Try oOcc = oCompDef.Occurrences.ItemByName("J channel LED edge:1") Catch 'Exit if the occurrence can't be found MsgBox("Couldn't find occurrence named J channel LED edge:1") Exit Sub End Try 'Get the named entities of J channel LED edge:1 Dim namedEntities = iLogicVb.Automation.GetNamedEntities(oOcc.Definition.Document) Dim S1Face As Face 'Get the face named J_Face Try S1Face= namedEntities.FindEntity("J_Face") Catch 'Exit if face cant be found MsgBox("Couldn't find face named J_Face") End Try 'Create a proxy object for the face Dim S1FaceProx As FaceProxy oOcc.CreateGeometryProxy(S1Face, S1FaceProx) 'Create geometry intent for the first drawingcurve segment of the curve representing the face. Dim oIntent As GeometryIntent = oSheet.CreateGeometryIntent(oView.DrawingCurves(S1FaceProx)(1)) 'Create a Point2d for the dimensions text position Dim oText As Point2d = ThisApplication.TransientGeometry.CreatePoint2d _ (oView.Center.X - oView.Width / 2, oView.Center.Y - oView.Height / 2 - 1) 'Add the dimension Dim oDim As LinearGeneralDimension = oSheet.DrawingDimensions.GeneralDimensions.AddLinear(oText, oIntent) 'Center the text oDim.CenterText 'Add the attribute set to recognize that the dimension is placed by iLogic oDim.AttributeSets.Add("iLogic_Created")
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
Thank you so very much @JhoelForshav your code worked perefectly well. I also expanded the code to take dismensions between two similar components with similar faces spaced apart and it also worked just fine. Many many thanks and appreciation that you took your precious time out to help me with my problem. ๐
I am posting my code here in case someone else needs it for dimensioning.
Just one last question for you, I am wondering if the parts are in the sub-assembly of my main assy, then I suppose I am gonna have to call the sub-assy in the code, right? Any chance if you can tell me how to accomplish that? I have just one sub-assy in my main assy and the dimensions are gonna be between them actually, so the code only has to run a loop to find any sub assy it can and use its components to get faces.
'Get the drawing Dim oDrawing As DrawingDocument = ThisDrawing.Document 'Get the active sheet Dim oSheet As Sheet = oDrawing.ActiveSheet 'Get the view named "FRONT" Dim oView As DrawingView For Each drwView As DrawingView In oSheet.DrawingViews If drwView.Name = "FRONT" oView = drwView Exit For End If Next 'Exit if view can't be found If oView Is Nothing MsgBox("Couldn't find the drawing view named FRONT") Exit Sub End If 'Get the assembly document from the view Dim oAsm As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument Dim oCompDef As AssemblyComponentDefinition = oAsm.ComponentDefinition Dim oOcc1 As ComponentOccurrence Dim oOcc2 As ComponentOccurrence 'Get the occurrence named J channel LED edge:1 Try oOcc1 = oCompDef.Occurrences.ItemByName("J channel LED edge:1") oOcc2 = oCompDef.Occurrences.ItemByName("J channel LED edge:2") Catch 'Exit if the occurrence can't be found MsgBox("Couldn't find occurrence named J channel LED edge:1") Exit Sub End Try 'Get the named entities of J channel LED edge:1 Dim namedEntities1 = iLogicVb.Automation.GetNamedEntities(oOcc1.Definition.Document) Dim namedEntities2 = iLogicVb.Automation.GetNamedEntities(oOcc2.Definition.Document) Dim S1Face As Face Dim S2Face As Face 'Get the face named J_Face Try S1Face = namedEntities1.FindEntity("J_Face") S2Face = NamedEntities2.FindEntity("J_Face") Catch 'Exit if face cant be found MsgBox("Couldn't find face named J_Face") End Try 'Create a proxy object for the face Dim S1FaceProx As FaceProxy Dim S2FaceProx As FaceProxy oOcc1.CreateGeometryProxy(S1Face, S1FaceProx) oOcc2.CreateGeometryProxy(S1Face, S2FaceProx) 'Create geometry intent for the first drawingcurve segment of the curve representing the face. Dim oIntent1 As GeometryIntent = oSheet.CreateGeometryIntent(oView.DrawingCurves(S1FaceProx)(1)) Dim oIntent2 As GeometryIntent = oSheet.CreateGeometryIntent(oView.DrawingCurves(S2FaceProx)(1)) 'Create a Point2d for the dimensions text position Dim oText As Point2d = ThisApplication.TransientGeometry.CreatePoint2d _ (oView.Center.X - oView.Width / 2, oView.Center.Y - oView.Height / 2 - 1) 'Add the dimension Dim oDim As LinearGeneralDimension = oSheet.DrawingDimensions.GeneralDimensions.AddLinear(oText, oIntent1, oIntent2) 'Center the text oDim.CenterText 'Add the attribute set to recognize that the dimension is placed by iLogic oDim.AttributeSets.Add("iLogic_Created")
Hi @sohaib.as01
Just replace "SubAssemblyName" with the name of your subassembly and it should work ๐
'Get the drawing Dim oDrawing As DrawingDocument = ThisDrawing.Document 'Get the active sheet Dim oSheet As Sheet = oDrawing.ActiveSheet 'Get the view named "FRONT" Dim oView As DrawingView For Each drwView As DrawingView In oSheet.DrawingViews If drwView.Name = "FRONT" oView = drwView Exit For End If Next 'Exit if view can't be found If oView Is Nothing MsgBox("Couldn't find the drawing view named FRONT") Exit Sub End If 'Get the assembly document from the view Dim oAsm As AssemblyDocument = oView.ReferencedDocumentDescriptor.ReferencedDocument Dim oCompDef As AssemblyComponentDefinition = oAsm.ComponentDefinition Dim oOcc1 As ComponentOccurrence Dim oOcc2 As ComponentOccurrence 'Get the occurrence named J channel LED edge:1 Try 'REPLACE "SubAssemblyName" with the name of your subassembly For Each oOcc As ComponentOccurrence In oCompDef.Occurrences.ItemByName("SubAssemblyName").SubOccurrences If oOcc.Name = "J channel LED edge:1" Then oOcc1 = oOcc If oOcc.Name = "J channel LED edge:2" Then oOcc2 = oOcc Next If oOcc1 Is Nothing Or oOcc2 Is Nothing MsgBox("Couldn't find the occurrences") Exit Sub End If Catch 'Exit if the occurrence can't be found MsgBox("Couldn't find SubAssembly") Exit Sub End Try 'Get the named entities of J channel LED edge:1 Dim namedEntities1 = iLogicVb.Automation.GetNamedEntities(oOcc1.Definition.Document) Dim namedEntities2 = iLogicVb.Automation.GetNamedEntities(oOcc2.Definition.Document) Dim S1Face As Face Dim S2Face As Face 'Get the face named J_Face Try S1Face = namedEntities1.FindEntity("J_Face") S2Face = namedEntities2.FindEntity("J_Face") Catch 'Exit if face cant be found MsgBox("Couldn't find face named J_Face") End Try 'Create a proxy object for the face Dim S1FaceProx As FaceProxy Dim S2FaceProx As FaceProxy oOcc1.CreateGeometryProxy(S1Face, S1FaceProx) oOcc2.CreateGeometryProxy(S1Face, S2FaceProx) 'Create geometry intent for the first drawingcurve segment of the curve representing the face. Dim oIntent1 As GeometryIntent = oSheet.CreateGeometryIntent(oView.DrawingCurves(S1FaceProx)(1)) Dim oIntent2 As GeometryIntent = oSheet.CreateGeometryIntent(oView.DrawingCurves(S2FaceProx)(1)) 'Create a Point2d for the dimensions text position Dim oText As Point2d = ThisApplication.TransientGeometry.CreatePoint2d _ (oView.Center.X - oView.Width / 2, oView.Center.Y - oView.Height / 2 - 1) 'Add the dimension Dim oDim As LinearGeneralDimension = oSheet.DrawingDimensions.GeneralDimensions.AddLinear(oText, oIntent1, oIntent2) 'Center the text oDim.CenterText 'Add the attribute set to recognize that the dimension is placed by iLogic oDim.AttributeSets.Add("iLogic_Created")
Jhoel Forshav
Download my free Inventor Addin - Hole Projector
LinkedIn | Ideas | Contributions | Blog posts | Website
okay @JhoelForshav got it, really appreciate you taking time out and helping me out. Cant thank you enough.
I was just wondering in this whole code we are hard coding the name of the components into the code, which is just perfect for me, to get faces, but I was wondering is there a way if we can tell the program to get say, 2nd or 3rd component from the model tree to get named entities? In that way we can make this code almost generic for every assembly. Or any other method similar to that? that will be wonderful, right?
Can't find what you're looking for? Ask the community or share your knowledge.