iLogic rule to fill in a sketch with certain pattern

iLogic rule to fill in a sketch with certain pattern

Anonymous
Not applicable
1,312 Views
11 Replies
Message 1 of 12

iLogic rule to fill in a sketch with certain pattern

Anonymous
Not applicable

Hi there again,

 

After getting solved our primary problem, (http://forums.autodesk.com/t5/inventor-customization/inventor-rectangular-equation/m-p/6067569#M6215... we got stuck with another issue.

 

The actual solution works very nice, but in rounded shapes, if we put a very high value we manage to fill any space. The problem is that we need to cut the continuos line to fit within the shape, and this will cut also any future rectangular pattern we may want to apply latter.


So my question is if it would be relatively easy to create sort of a zig zag line  (following previous posts rule) in only a quarter of a circle so the whole quarter is filled with a continuous line, so we could apply a rectangular pattern later.


I am used to VB net language, but the inventor API is completely new for me, so so sorry if I cannot be proficient with the code right now.

 

I think the rule will be someting using the same principle of the previous post, but doing some border conditions to continue creating points.

 

It doesnt need to follow an spiral, another "rule" can be done to fill in with points following the base distance between them and forming 60 and 120 degress between them.


Any suggestion?

 

TIP:? have added a picture of what area I would love to fill continuously.


Thank you so much in advance!

 

0 Likes
1,313 Views
11 Replies
Replies (11)
Message 2 of 12

Owner2229
Advisor
Advisor

Hi, might this (picture in attachment) be what you want? Or something else?

I have made just a part of it, it should fill the whole circle like this.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 3 of 12

Anonymous
Not applicable

Hi there again!

 

****, I should invite you to something Owner! It is exactly what we want!

 

Thank you!

0 Likes
Message 4 of 12

Anonymous
Not applicable

Hi again,

 

Sorry for the double posting.


I have checked it further and it is almost there. There is also one more limitation, as the line should follow also the 1 1 2 2 3 3 4 4 times the original dimension, and it is likely this wont happen when touching the border (as a function of PI it is unlikely it will match the original dimension constrain. Thus, sometimes line will be shorter before touching the border.

 

I think given the skill you showed until now this will be so easy. I am trying to check the code in order to learn.


Thank you so much!


TIP: I am attaching a picture showing the limits

0 Likes
Message 5 of 12

Anonymous
Not applicable

Hi,

I am adding the logic mentioned before. As you can see, following the rule not all the lines touches the border. If basic fixed distance (or a multiple of) is trespassed, doesnt continue, and "cuts" before, repeating the pattern.

 

This is my idea.

 

Thank you!

0 Likes
Message 6 of 12

Owner2229
Advisor
Advisor

Hi, why is there the need to keep the multiplication of  the original dimension?

How will it be in the second quorder of the circle? The multiplication will decrease?

In the half (of the halp circle) we have to start decreasing the lenght of the line that goes up (under angle) or we'll get a triangle instead of a circle.

 

If you mean to cut the line before it goes out of the circle, then how far from the end of the "originaly intended ending point" (where it would ussualy go acording to the multiplication of the first dimension) you want it to be offset? Again by multiplication of the first dimension (that much to get it in the circle) or just that much to touch the circle?

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 7 of 12

Anonymous
Not applicable

Hi Owner,


The reason is that we are going to do a rectangular pattern later following a fixed distance, so if no fixed distance, the pattern could not match with the borders of the original distance!

 

Regarding the second quarter, it is really not needed, as you can always do a mirrowing on that!

 

Well, concerning the offset you want to consider, just the last multiplication that fits without trespassing the border, thats enough.

 

So in the example I showed u, the first one is 1, then 2, because if u add a third one it goes outside the circle, then next one.... For the angle, of sucessive ones, at the moment i just followed 0, but could be also 30, so we fill the most available space within.


So sorry if i didnt managed to explain myselft properly.

 

Kindly yours!

0 Likes
Message 8 of 12

Anonymous
Not applicable

Hi there again,

 

After being struggling with the original code provided by Owner, we had got some changes into the code, although it still doesnt fit.


Please find attached the png file with the result, which is awful, but we keep trying. My trigonometry is a little bit rusty so I need to recall some things... apart from the logic itself. Any idea will be welcomed. I am trying just to make all the points within the circle, following the basic equation for a circle. My bottom limit is the X axis and my vertical limit us the Y axis (just one quarter filling)

 

We only want to fill a quarter of the circle....

 

This is our actual code used:

 

Sub Main()
    Dim oDoc As Document = ThisApplication.ActiveDocument
    Dim oCD As ComponentDefinition = oDoc.ComponentDefinition
    If TypeOf ThisApplication.ActiveEditObject Is Sketch Then
        oSketch = ThisApplication.ActiveEditObject
    Else
        MsgBox("Run this rule from an open sketch, please.")
        Exit Sub
    End If
	Try
	    oArea = InputBox("Please enter the diameter to be filled.", "Area input", 1000)
        If oArea = 0 Then
            Exit Sub
        End If
	    OrigSize = InputBox("Please enter the pitch.", "Area input", 25)
        If OrigSize = 0 Then
            Exit Sub
        End If
		oLimit = InputBox("Please enter the diameter to be filled.", "Area input", 1000)
	    If oLimit = 0 Then
            Exit Sub
        End If
	Catch
		Exit Sub
	End Try
    CreateLines()
    oSketch.ExitEdit
    InventorVb.DocumentUpdate()
End Sub

Private Amount As Integer
Private oArea As Double
Private oLine As SketchLine
Private oLimit As Double
Private OrigSize As Double
Private oSketch As Sketch

Private Sub CreateLines()
    Dim PI As Double = 4 * Atan(1)
	Dim TG As TransientGeometry = ThisApplication.TransientGeometry
	oArea = oArea * 0.1 'cm to mm
	OrigSize = OrigSize * 0.1 'cm to mm
	oSize = OrigSize
	Dim PosX As Double = -oLimit * 0.1 / 2 + OrigSize
	Dim PosY As Double = 0
	Dim Amount As Integer = Floor(oArea / (oSize * 0.7473))
	Dim Point1 As Point2D = TG.CreatePoint2d(-oLimit * 0.1 / 2, 0)
	Dim Point2 As Point2D
	Dim Position As Integer = 1
		
	For i = 1 To Amount
		Select Case Position
		Case 1
			Point2 = TG.CreatePoint2d(PosX, PosY)
			Try
				oLine = oSketch.SketchLines.AddByTwoPoints(oLine.EndSketchPoint, Point2)
			Catch
				oLine = oSketch.SketchLines.AddByTwoPoints(Point1, Point2)
			End Try
			oSketch.GeometricConstraints.AddGround(oLine)
		Case 2
				PosX = PosX - (oSize * Sin(PI / 6))
				PosY = PosY + (oSize * Cos(PI / 6))
			For n=0 To 100 Step 1
				If Abs(PosX) > Sqrt((oLimit * 0.1)^2 - PosY^2) And Abs(PosY) > Sqrt((oLimit * 0.1)^2 - PosX^2) Then
					Exit For
				Else
					PosX = PosX - n * (oSize * Sin(PI / 6))
					PosY = PosY + n * (oSize * Cos(PI / 6))
				End If
			Next
			Point2 = TG.CreatePoint2d(PosX, PosY)
			AddLine(Point2)
		Case 3
			oSize = OrigSize
			PosX = PosX + oSize * Sin(PI / 6)
			PosY = PosY + oSize * Cos(PI / 6)
			Point2 = TG.CreatePoint2d(PosX, PosY)
			AddLine(Point2)
		Case 4
			PosX = PosX + 2 * (oSize * Sin(PI / 6))
			PosY = PosY - 2 * (oSize * Cos(PI / 6))
			Point2 = TG.CreatePoint2d(PosX, PosY)
			AddLine(Point2)
			oSize = OrigSize
			PosX = PosX + oSize
			PosY = PosY
		End Select
		If Position = 4 Then Position = 1 Else Position = Position + 1
	Next
End Sub

Private Sub AddLine(PointB As Point2D)
	oLine = oSketch.SketchLines.AddByTwoPoints(oLine.EndSketchPoint, PointB)
	oSketch.GeometricConstraints.AddGround(oLine)
End Sub


 

0 Likes
Message 9 of 12

Anonymous
Not applicable

Dear all,

 

After struggling a little bit more, we got an approach which is following the border somehow.

 

Please find attached also the picture. Lets see if we continue tweaking the code.

 

Here is the updated code:

 

Sub Main()
    Dim oDoc As Document = ThisApplication.ActiveDocument
    Dim oCD As ComponentDefinition = oDoc.ComponentDefinition
    If TypeOf ThisApplication.ActiveEditObject Is Sketch Then
        oSketch = ThisApplication.ActiveEditObject
    Else
        MsgBox("Run this rule from an open sketch, please.")
        Exit Sub
    End If
	Try
	    oArea = InputBox("Please enter the diameter to be filled.", "Area input", 1000)
        If oArea = 0 Then
            Exit Sub
        End If
	    OrigSize = InputBox("Please enter the pitch.", "Area input", 25)
        If OrigSize = 0 Then
            Exit Sub
        End If
		oLimit = InputBox("Please enter the diameter to be filled.", "Area input", 1000)
	    If oLimit = 0 Then
            Exit Sub
        End If
	Catch
		Exit Sub
	End Try
    CreateLines()
    oSketch.ExitEdit
    InventorVb.DocumentUpdate()
End Sub

Private Amount As Integer
Private oArea As Double
Private oLine As SketchLine
Private oLimit As Double
Private OrigSize As Double
Private oSketch As Sketch

Private Sub CreateLines()
    Dim PI As Double = 4 * Atan(1)
	Dim TG As TransientGeometry = ThisApplication.TransientGeometry
	oArea = oArea * 0.1 'cm to mm
	OrigSize = OrigSize * 0.1 'cm to mm
	oSize = OrigSize
	Dim PosX As Double = -oLimit * 0.1 / 2 + OrigSize
	Dim PosY As Double = 0
	Dim LastGoodPosX As Double
	Dim LastGoodPosY As Double
	Dim PassSin as Double = (oSize * Sin(PI / 6))
	Dim PassCos as Double = (oSize * Cos(PI / 6))
	Dim Amount As Integer = Floor(oArea / (oSize * 0.7473))
	Dim Point1 As Point2D = TG.CreatePoint2d(-oLimit * 0.1 / 2, 0)
	Dim Point2 As Point2D
	Dim Position As Integer = 1
	Dim TotalDistance As Integer
	Dim Exceeded As Boolean
	
	For i = 1 To Amount
		Select Case Position
		Case 1
			Point2 = TG.CreatePoint2d(PosX, PosY)
			Try
				oLine = oSketch.SketchLines.AddByTwoPoints(oLine.EndSketchPoint, Point2)
			Catch
				oLine = oSketch.SketchLines.AddByTwoPoints(Point1, Point2)
			End Try
			oSketch.GeometricConstraints.AddGround(oLine)
		Case 2
				For n=1 To 100 Step 1
				PosX = PosX - n * PassSin
				PosY = PosY + n * PassCos
				TotalDistance = n
				If PosX^2 + PosY^2 > (oLimit * 0.1 / 2)^2 Then
					PosX = LastGoodPosX
					PosY = LastGoodPosY
					TotalDistance = n-1
					Exceeded = True
					Exit For
				Else
					Exceeded = False
				End If
				LastGoodPosX = PosX
				LastGoodPosY = PosY
			Next
			Point2 = TG.CreatePoint2d(PosX, PosY)
			AddLine(Point2)
		Case 3
			If Exceeded = True Then
			PosX = LastGoodPosX + Passsin
			PosY = LastGoodPosY + Passcos
			Else
			PosX = PosX + PassSin
			PosY = PosY + PassCos
			End If
			Point2 = TG.CreatePoint2d(PosX, PosY)
			AddLine(Point2)
		Case 4
			PosX = PosX + (TotalDistance+1) * (PassSin)
			PosY = PosY - (TotalDistance+1) * (PassCos)
			Point2 = TG.CreatePoint2d(PosX, PosY)
			AddLine(Point2)
			oSize = OrigSize
			PosX = PosX + oSize
			PosY = PosY
		End Select
		If Position = 4 Then Position = 1 Else Position = Position + 1
	Next
End Sub

Private Sub AddLine(PointB As Point2D)
	oLine = oSketch.SketchLines.AddByTwoPoints(oLine.EndSketchPoint, PointB)
	oSketch.GeometricConstraints.AddGround(oLine)
End Sub
0 Likes
Message 10 of 12

Anonymous
Not applicable

Hi there,

 

We keep trying.


After some more brainstorming we have got something really close to what we want. please see picture.


RIght now the only thing we want to do is to continue with the y axis. We managed to stop there, and we are struggling to get the part of the code that will allow it to finish it. Please feel free to comment. 

 

We should thank Owner before for his suggestion. Code is based on his, although is quite dirty at the moment, partially works.

 

This is the code:

 

Sub Main()
    Dim oDoc As Document = ThisApplication.ActiveDocument
    Dim oCD As ComponentDefinition = oDoc.ComponentDefinition
    If TypeOf ThisApplication.ActiveEditObject Is Sketch Then
        oSketch = ThisApplication.ActiveEditObject
    Else
        MsgBox("Run this rule from an open sketch, please.")
        Exit Sub
    End If
	Try
	    oArea = InputBox("Please enter the diameter to be filled.", "Area input", 1000)
        If oArea = 0 Then
            Exit Sub
        End If
	    OrigSize = InputBox("Please enter the pitch.", "Area input", 25)
        If OrigSize = 0 Then
            Exit Sub
        End If
		oLimit = InputBox("Please enter the diameter to be filled.", "Area input", 1000)
	    If oLimit = 0 Then
            Exit Sub
        End If
	Catch
		Exit Sub
	End Try
    CreateLines()
    oSketch.ExitEdit
    InventorVb.DocumentUpdate()
End Sub

Private Amount As Integer
Private oArea As Double
Private oLine As SketchLine
Private oLimit As Double
Private OrigSize As Double
Private oSketch As Sketch

Private Sub CreateLines()
    Dim PI As Double = 4 * Atan(1)
	Dim TG As TransientGeometry = ThisApplication.TransientGeometry
	oArea = oArea * 0.1 'cm to mm
	OrigSize = OrigSize * 0.1 'cm to mm
	oSize = OrigSize
	Dim PosX As Double = -oLimit * 0.1 / 2 + OrigSize
	Dim PosY As Double = 0
	Dim LastGoodPosX As Double
	Dim LastGoodPosY As Double
	Dim PassSin as Double = (oSize * Sin(PI / 6))
	Dim PassCos as Double = (oSize * Cos(PI / 6))
	Dim Amount As Integer = Floor(oArea / (oSize * 0.7473))
	Dim Point1 As Point2D = TG.CreatePoint2d(-oLimit * 0.1 / 2, 0)
	Dim Point2 As Point2D
	Dim Position As Integer = 1
	Dim TotalDistance As Integer
	Dim Exceeded As Boolean
	Dim Totalheight As Integer
	Dim ActualXDistance As Integer
	Dim ReversedCase As Boolean
	
	For i = 1 To Amount
		Select Case Position
		Case 1
			Point2 = TG.CreatePoint2d(PosX, PosY)
			Try
				oLine = oSketch.SketchLines.AddByTwoPoints(oLine.EndSketchPoint, Point2)
			Catch
				oLine = oSketch.SketchLines.AddByTwoPoints(Point1, Point2)
			End Try
			oSketch.GeometricConstraints.AddGround(oLine)
		Case 2
				For n=1 To 100 Step 1
				PosX = PosX - n * PassSin
				PosY = PosY + n * PassCos
				TotalDistance = n
				If PosX^2 + PosY^2 > (oLimit * 0.1 / 2)^2 Then
					PosX = LastGoodPosX
					PosY = LastGoodPosY
					TotalDistance = n-1
					Exceeded = True
					Exit For
				Else
					Exceeded = False
				End If
				LastGoodPosX = PosX
				LastGoodPosY = PosY
			Next
			Point2 = TG.CreatePoint2d(PosX, PosY)
			AddLine(Point2)
		Case 3
			If Exceeded = True Then
			PosX = LastGoodPosX + Passsin
			PosY = LastGoodPosY + Passcos
			Else
			PosX = PosX + PassSin
			PosY = PosY + PassCos
			End If
			Point2 = TG.CreatePoint2d(PosX, PosY)
			AddLine(Point2)
		Case 4
			If ReversedCase = False Then
			Totalheight = Floor( PosY / PassCos )
			PosX = PosX + Totalheight * (PassSin)
			PosY = PosY - Totalheight * (PassCos)
			Point2 = TG.CreatePoint2d(PosX, PosY)
			AddLine(Point2)
			End If
		Case 5
			ActualXDistance = Floor ( PosX / PassSin)
			If Abs(ActualXDistance) <= osize Then
			ReversedCase = True
			PosX = PosX + PassSin
			PosY = PosY + PassCos
			Point2 = TG.CreatePoint2d(PosX, PosY)
			AddLine(Point2)
			Else
			ReversedCase = False
			End If
			oSize = OrigSize
			PosX = PosX + oSize
			PosY = PosY
			End Select
		If Position = 5 Then Position = 1 Else Position = Position + 1
	Next
End Sub

Private Sub AddLine(PointB As Point2D)
	oLine = oSketch.SketchLines.AddByTwoPoints(oLine.EndSketchPoint, PointB)
	oSketch.GeometricConstraints.AddGround(oLine)
End Sub
0 Likes
Message 11 of 12

Owner2229
Advisor
Advisor

Hi, I've got some time to look at your code and here below is my result:

It's not complete, but it's a good start.

You've been overly complicating it. You dont need to check if the line is out of the diameter. You need to compute the lenght of the line from the given point to the circle and then round it to match the "Pitch".

 

PS.: Don't call me "Owner", it's just a nickname. I'm Mike, as it stands in my signature 😉

 

Sub Main()
    Dim oDoc As Document = ThisApplication.ActiveDocument
    If TypeOf ThisApplication.ActiveEditObject Is Sketch Then
        oSketch = ThisApplication.ActiveEditObject
    Else
        MsgBox("Run this rule from an open sketch, please.")
        Exit Sub
    End If
	Try
	    oArea = InputBox("Please enter the diameter to be filled.", "Area input", 1000)
        If oArea = 0 Then
            Exit Sub
        End If
	    OrigSize = InputBox("Please enter the pitch.", "Pitch input", 25)
        If OrigSize = 0 Then
            Exit Sub
        End If
	Catch
		Exit Sub
	End Try
    CreateLines()
    oSketch.ExitEdit
    InventorVb.DocumentUpdate()
End Sub

Private Amount As Integer
Private oArea As Double
Private oLine As SketchLine
Private OrigSize As Double
Private oSketch As Sketch
Private oRadius As Double
Private angA As Double
Private First As Boolean

Private Sub CreateLines()
    Dim PI As Double = 4 * Atan(1)
	Dim TG As TransientGeometry = ThisApplication.TransientGeometry
	oArea = oArea * 0.1 'cm to mm
	oRadius = oArea * 0.5
	OrigSize = OrigSize * 0.1 'cm to mm
	Dim PosX As Double = -oRadius
	Dim PosY As Double = 0
	Dim LastPosX As Double = -oRadius
	Dim PassSin as Double = (OrigSize * Sin(PI / 3))
	Dim PassCos as Double = (OrigSize * Cos(PI / 3))
	Dim Amount As Integer = Floor(oArea / (OrigSize * 2)) * 4
	Dim Point1 As Point2D = TG.CreatePoint2d(-oRadius, 0)
	Dim Point2 As Point2D
	Dim Position As Integer = 1
	Dim Sin60 As Double = Sin(PI / 3)
	Dim Cos60 As Double = Cos(PI / 3)
	angA = (PI / 3) * 2
	First = True
	For i = 1 To Amount
		Select Case Position
		Case 1
			LastPosX = LastPosX + OrigSize
			PosX = LastPosX
			Point2 = TG.CreatePoint2d(LastPosX, PosY)
			Try
				oLine = oSketch.SketchLines.AddByTwoPoints(oLine.EndSketchPoint, Point2)
			Catch
				oLine = oSketch.SketchLines.AddByTwoPoints(Point1, Point2)
			End Try
			oSketch.GeometricConstraints.AddGround(oLine)
		Case 2
			Dim oLenght As Double = oLineLenght(LastPosX)
			PosX = PosX - (Cos60 * oLenght)
			PosY = Sin60 * oLenght
			Point2 = TG.CreatePoint2d(PosX, PosY)
			AddLine(Point2)
		Case 3
			PosX = PosX + PassSin
			PosY = PosY + PassCos
			Point2 = TG.CreatePoint2d(PosX, PosY)
			AddLine(Point2)
		Case 4
			LastPosX = LastPosX + OrigSize
			PosY = 0
			Point2 = TG.CreatePoint2d(LastPosX, PosY)
			AddLine(Point2)
		End Select
		If Position = 4 Then Position = 1 Else Position = Position + 1
	Next
End Sub

Private Sub AddLine(PointB As Point2D)
	oLine = oSketch.SketchLines.AddByTwoPoints(oLine.EndSketchPoint, PointB)
	oSketch.GeometricConstraints.AddGround(oLine)
End Sub

Private Function oLineLenght(Distance As Double) As Double
	Dim angB As Double = ASin(-Distance / (oRadius / Sin(angA)))
	Dim angC As Double = (PI - angA) - angB
	Dim maxLenght As Double = (oRadius / Sin(angA)) * Sin(angC)
	Dim oLineAmount As Double = Floor(maxLenght / OrigSize)
	If Distance > 0 Then
		oLineAmount = oLineAmount - 1
		If First Then
			oLineAmount = oLineAmount + 1
			First = False
		End If
		If Distance >= (oRadius - (OrigSize * 3)) And Distance < (oRadius - (OrigSize * 2)) Then
			oLineAmount = oLineAmount - 1
		End If
	End If
	oLineLenght = oLineAmount * OrigSize
End Function

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 12 of 12

Owner2229
Advisor
Advisor

There've been some issues with the 3th line, so I've changed it to be under 30° instead of 60° and wrote some exeptions in the "oLineLenght" function, starting with "If First Then". It might be better to start drawing this complex line from right instead of left. It would then measure the 4th line instad of the 2th line, so the 3th line wouldn't cause the issues.

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes